Prebaked
Integration

Netlify Edge Function

Last reviewed

A Netlify Edge Function on every path of your site sends search engines, AI crawlers and link preview bots to Prebaked, and everyone else to your site unchanged. Use it for a JavaScript app or single-page app hosted on Netlify. You add two files to your repository and two environment variables.

How it works

Visitor requests continue to your site. Crawler requests go through the Edge Function to Prebaked, which returns the fully rendered page.

Same rules as the Cloudflare Worker:

  • Prerenders only GET / HEAD requests from a known crawler user agent. Static files, other methods and requests carrying X-Prerender go to your site.
  • Calls GET https://service.prebaked.io/<full page URL> with X-Prerender-Token.
  • Passes Prebaked's 200, redirects, 404 and 410 to the crawler.
  • Serves your normal page (context.next()) when Prebaked cannot be reached, takes longer than 5 seconds, or answers 401/403, 400 or 5xx.

Prerequisites

  • A Prebaked account with your domain added and verified.
  • Your domain token from the domain's Setup page.
  • A Netlify site deployed from a repository you can change, and the Netlify UI or CLI to set environment variables.

Step 1: Copy the files

Into your site repository, keeping the paths:

mkdir -p netlify/edge-functions netlify/lib
curl -o netlify/edge-functions/prebaked.ts https://prebaked.io/integrations/files/netlify/prebaked.ts
curl -o netlify/lib/prebaked-edge.ts https://prebaked.io/integrations/files/netlify/prebaked-edge.ts

Step 2: Set the environment variables

In the Netlify UI (Site configuration > Environment variables) or with the CLI:

netlify env:set PREBAKED_SERVICE_URL https://service.prebaked.io/
netlify env:set PREBAKED_TOKEN <your token> --secret

Without either variable every request goes to your site.

Step 3: Deploy and check

Deploy as usual, then:

curl -A "Googlebot" https://www.<your-domain>/        # rendered HTML
curl https://www.<your-domain>/                      # your normal page

Then check which side answered:

curl -sI -A "GPTBot" https://www.<your-domain>/ | grep -i x-prebaked
  • x-prebaked: hit: Prebaked answered from its cache.
  • x-prebaked: stale: Prebaked answered from an older cached copy and refreshes it in the background.
  • x-prebaked: rendered: Prebaked rendered the page for this request.
  • x-prebaked: origin: Prebaked fetched your page itself without rendering it, for example when the render took too long.
  • No output: your site answered directly. The integration is not active yet, the user agent did not match, or the fallback served your page.

The first crawler request appears in the Prebaked dashboard request log.

Test the installation

The curl commands in step 3 show the difference: as Googlebot you get the full HTML, as a visitor your normal page. The first request for a page can take a few seconds while Prebaked renders it; later requests come from the cache. Then run the free checker on the same page.

Markdown requests

Turning it on per domain, choosing pages and testing: Agent-ready responses.

Requests that ask for Markdown go to Prebaked whatever the user agent: a path ending in .md (any case), or an Accept header that prefers text/markdown over text/html (*/* alone never does). They are sent with X-Prebaked-Formats: markdown and the visitor's Accept. When Markdown is off or unavailable for the page (X-Prebaked-Markdown in the answer), or Prebaked fails or is slow, the request goes on to your site unchanged, so your own .md files keep working; a crawler that asked by Accept only gets the normal prerendered HTML. Test mode shadow leaves Markdown requests untouched; ai routes them for every user agent. Check:

curl -sI https://www.<your-domain>/pricing.md
curl -sI -H 'Accept: text/markdown' https://www.<your-domain>/pricing

Test mode

Test Prebaked while your current prerender setup keeps answering crawlers. You do not need your current service's URL or token:

netlify env:set PREBAKED_MODE shadow        # then "ai", then "all"
  • shadow: every request continues to your site untouched (context.next()), so your current prerender setup keeps answering crawlers. Prebaked gets a background copy (context.waitUntil), renders and caches the page; its answer is discarded.
  • ai: Prebaked answers AI crawlers (GPTBot, OAI-SearchBot, ChatGPT-User, ClaudeBot, Claude-User, PerplexityBot, ...); every other request continues to your site untouched.
  • all (default): Prebaked answers every crawler.

Redeploy after each change. Roll back by setting the previous mode.

Add-on for an existing prerender Edge Function

If your current prerender service runs as a Netlify Edge Function, keep it and do not install this integration on the same route. Paste this function into your existing script once:

// Prebaked shadow add-on: sends a background copy of each crawler page request to Prebaked.
// Your response is not changed or delayed.
const PREBAKED_BOTS =
  /GPTBot|OAI-SearchBot|ChatGPT-User|ClaudeBot|Claude-User|Claude-SearchBot|CCBot|PerplexityBot|Perplexity-User|meta-externalagent|Bytespider|Googlebot|Applebot|bingbot|facebookexternalhit|TelegramBot|Twitterbot|LinkedInBot|Slackbot-LinkExpanding|Discordbot|WhatsApp/i;
const PREBAKED_STATIC = /\.(?:js|css|xml|png|jpe?g|gif|svg|ico|webp|avif|woff2?|ttf|otf|eot|pdf|txt|json|map|mp4|zip)$/i;
function prebakedShadow(request, token, waitUntil) {
  const ua = request.headers.get('user-agent') || '';
  if (!token || request.method !== 'GET' || request.headers.has('x-prerender')) return;
  if (!PREBAKED_BOTS.test(ua) || PREBAKED_STATIC.test(new URL(request.url).pathname)) return;
  const copy = fetch('https://service.prebaked.io/' + request.url, {
    headers: { 'x-prerender-token': token, 'x-prebaked-mode': 'shadow', 'user-agent': ua },
  });
  waitUntil(copy.then((res) => res.body?.cancel(), () => undefined));
}

Then add this line at the start of your request handler:

prebakedShadow(request, Netlify.env.get('PREBAKED_TOKEN'), (p) => context.waitUntil(p));

Your current script keeps answering crawlers; Prebaked only gets the background copy. When you are ready for ai or all, replace your script with this integration. Details: Test before you switch.

Common errors

What you see Cause Fix
Crawlers still get your empty page PREBAKED_SERVICE_URL or PREBAKED_TOKEN is not set, so every request goes to your site Set both variables and redeploy
Crawlers get your normal page, Prebaked logs a 401 Wrong or old token Copy the current token from the Setup page and set PREBAKED_TOKEN again
Crawlers get your normal page, Prebaked logs a 403 The domain is not verified yet Verify the domain in the dashboard
The function does not run The files are not at netlify/edge-functions/ and netlify/lib/ Keep the paths from step 1

More help: Crawlers still see an empty page and 401 and 403 errors.

Fallback and security

If Prebaked cannot be reached, answers an error or takes over 5 seconds, crawlers get your normal page. Prebaked gets the page URL, the crawler's user agent, your token and the visitor IP, not the visitor's cookies.

  • The visitor IP (context.ip) is sent as X-Forwarded-For so Prebaked can verify crawler IPs.
  • Imports use the .ts extension, as Deno (the Edge Functions runtime) requires.

Removing it

Delete netlify/edge-functions/prebaked.ts and netlify/lib/prebaked-edge.ts, remove the PREBAKED_* environment variables and redeploy. Every request then goes straight to your site.

FAQ

Does the Edge Function slow down my site for visitors?

No. For visitors it continues to your site without calling Prebaked.

What happens if Prebaked is down?

Crawlers get your normal page. The function waits at most 5 seconds for Prebaked and also falls back on errors such as 401, 403 and 5xx.

Can I test Prebaked next to my current prerender service?

Yes. Set PREBAKED_MODE to shadow, or paste the add-on into your current Edge Function. Your current setup keeps answering crawlers.