Prebaked
Integration

Cloudflare Worker

Last reviewed

A Cloudflare Worker on your site's route sends search engines, AI crawlers and link preview bots to Prebaked, and everyone else to your site unchanged. Use it when your domain runs through Cloudflare, whatever your origin is: a JavaScript app, a site builder or your own server. The dashboard can install the Worker for you in one step; you can also deploy it yourself with wrangler.

How it works

Visitor requests go straight to your origin. Crawler requests go through the Worker to Prebaked, which returns the fully rendered page.

  • Prerenders only GET / HEAD requests from a known crawler user agent. Static files (.js, .css, images, fonts, ...), other methods and requests carrying X-Prerender (Prebaked's own page fetches) go straight to your origin.
  • Calls GET https://service.prebaked.io/<full page URL> with your token in X-Prerender-Token.
  • Passes Prebaked's 200, redirects, 404 and 410 to the crawler (the page's own status).
  • Serves your origin page instead when Prebaked cannot be reached, takes longer than 5 seconds, or answers 401/403 (token or domain problem), 400 or 5xx. Crawlers never see a Prebaked error.
  • Sends Prebaked only the crawler's user agent, the token and the visitor IP, never cookies or credentials; a Set-Cookie from Prebaked is not passed on.

Prerequisites

  • A Prebaked account with your domain added and verified.
  • The domain proxied through Cloudflare (orange cloud), so a Worker route can run in front of it.
  • For the manual setup: your domain token from the domain's Setup page, and Node.js to run npx wrangler.

One-click install from the dashboard

Open your domain's Setup page in the Prebaked dashboard and use Connect Cloudflare:

  • Connect with Cloudflare: sign in to Cloudflare and approve access. Or
  • Use an API token: create a Cloudflare API token from the pre-filled link with these permissions: Account > Workers Scripts > Edit, Zone > Workers Routes > Edit, Zone > Zone > Read, Zone > DNS > Read. Limit it to your account and your domain's zone, paste it and press Install.

Prebaked uploads a Worker named prebaked-<your domain>, adds the route for your domain (<your-domain>/*, plus www.<your-domain>/* when the domain includes www) and sets the token as a Worker secret. Your Cloudflare credential is used once and never stored. Routes that already run another Worker are never changed.

Connecting creates a new token for the domain and puts it in the Worker. The previous token stops working, so update any other install of the same domain afterwards.

To try Prebaked next to your current prerender service, turn on Start as a test (keep my current service) before you connect. The Worker then starts in shadow mode, see Test mode.

Step 1: Copy the files

The manual setup gives you the same Worker in your own Cloudflare project.

Into a Worker project (npm create cloudflare@latest, or an existing one):

mkdir -p src
curl -o src/worker.ts https://prebaked.io/integrations/files/cloudflare-worker/worker.ts
curl -o src/prebaked-edge.ts https://prebaked.io/integrations/files/cloudflare-worker/prebaked-edge.ts
curl -o wrangler.toml https://prebaked.io/integrations/files/cloudflare-worker/wrangler.toml

Step 2: Configure

In wrangler.toml:

main = "src/worker.ts"
routes = [{ pattern = "www.<your-domain>/*", zone_name = "<your-domain>" }]

[vars]
PREBAKED_SERVICE_URL = "https://service.prebaked.io/"

Store the token as a secret, not in wrangler.toml:

npx wrangler secret put PREBAKED_TOKEN

Step 3: Deploy

npx wrangler deploy

Without PREBAKED_SERVICE_URL or PREBAKED_TOKEN the Worker passes every request to your origin.

Test the installation

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.

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 to see what an AI crawler gets.

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. Three modes:

PREBAKED_MODE Crawlers get Prebaked
shadow your origin, untouched, so your current prerender setup keeps answering gets a background copy of each crawler request (ctx.waitUntil), renders and caches it; its answer is discarded
ai Prebaked for AI crawlers (GPTBot, OAI-SearchBot, ChatGPT-User, ClaudeBot, Claude-User, PerplexityBot, ...); every other request goes on to your origin untouched answers AI crawlers
all Prebaked answers every crawler
unset or auto the mode set in the Prebaked dashboard the Worker asks Prebaked, at most once a minute; if that fails it uses all

Set the mode in wrangler.toml:

[vars]
PREBAKED_SERVICE_URL = "https://service.prebaked.io/"
PREBAKED_MODE = "shadow"            # or "ai", "all"; remove it to follow the dashboard
npx wrangler deploy

The background copy never delays or changes what crawlers get. Roll back by setting the previous mode.

Add-on for an existing prerender Worker

If your current prerender service runs as a Cloudflare Worker, 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, env.PREBAKED_TOKEN, (p) => ctx.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 missing, so the Worker passes every request to your origin Check [vars] in wrangler.toml and run npx wrangler secret put PREBAKED_TOKEN again
Crawlers get your normal page, Prebaked logs a 401 Wrong or old token, for example after a one-click connect created a new one Copy the current token from the Setup page and store it as the secret again
Crawlers get your normal page, Prebaked logs a 403 The domain is not verified yet Verify the domain in the dashboard
One-click install stops at the route step The route already runs another Worker Remove or move that Worker, or use the add-on to test next to it
Nothing reaches the Worker The domain is not proxied through Cloudflare, or the route pattern does not match your hostname Turn on the proxy for the DNS record and check routes in wrangler.toml

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

Fallback and security

The Worker never makes things worse than they are without it: if Prebaked fails, crawlers get your origin page.

  • The visitor IP (CF-Connecting-IP) is sent as X-Forwarded-For, so Prebaked can verify that a request claiming to be Googlebot really comes from Google.
  • The 5 second limit covers the whole Prebaked response. Cached pages answer well within it.

Removing it

  • One-click install: open Connect Cloudflare on the Setup page, choose Remove… and paste a Cloudflare token with the same permissions. Prebaked removes its Worker and the route.
  • Manual install: delete the route or the Worker in the Cloudflare dashboard, or run npx wrangler delete in the Worker project.

Requests then go straight to your origin again, for crawlers too.

FAQ

Does the Worker slow down my site for visitors?

No. For visitors the Worker passes the request on to your origin without calling Prebaked.

What happens if Prebaked is down?

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

I already run a prerender Worker from another service. Can I test Prebaked?

Yes. Keep your Worker and paste the add-on into it. Your Worker keeps answering crawlers, and Prebaked only gets a background copy of each crawler request.

Which crawlers get the rendered page?

Search engines, AI crawlers and link preview bots from the crawler list. Everyone else gets your site.