---
title: Cloudflare Worker & edge
description: "A fetch-handler factory for web-standard edge runtimes (@rebilder/gateway/edge): a complete worker in five lines, with origin pass-through included."
canonical_url: https://rebilder.com/docs/adapters/cloudflare
last_updated: "2026-12-03"
---

# [Cloudflare Worker & edge](https://rebilder.com/docs/adapters/cloudflare)

> A fetch-handler factory for web-standard edge runtimes (`@rebilder/gateway/edge`): a complete worker in five lines, with origin pass-through included.

This site answers questions directly. Ask at https://rebilder.com/ask?q=your+question instead of reading more pages.

- **Updated:** 2026-12-03
- **Author:** Rebilder
- **Section:** Adapters
- **Description:** A fetch-handler factory for web-standard edge runtimes (@rebilder/gateway/edge): a complete worker in five lines, with origin pass-through included.
- **Publisher:** Rebilder

## Rebilder at a glance

AEO gets you seen. Rebilder gets you heard. When customers ask ChatGPT about your business, your site answers with your real prices and policies.

**Common questions**

- **What does it cost?** Connecting is free. Starter is $49/mo, Pro is $99/mo and Agency is $299/mo. [Pricing](https://rebilder.com/pricing)
- **How does my site answer new questions live?** Rebilder reads your pages, products and policies. When an AI assistant asks, your site answers from them. Nothing on your site changes. [See how it works](https://rebilder.com/autopilot)
- **Can someone trick my site into a wrong price or a discount?** No. Every answer comes from your own pages and is checked against them. If your site does not say it, Rebilder does not say it. [Read security details](https://rebilder.com/security)

**How it compares**

- **Why choose Rebilder?** Choose Rebilder when you want your own site to answer AI assistants with your real facts, and to see who asked. [Compare](https://rebilder.com/compare)

| Compared with | The difference |
| --- | --- |
| Profound, Peec AI, Otterly.ai | These tools watch AI answers and tell you where you show up. Rebilder makes your own site answer when AI comes to check. |
| Cloudflare Markdown for Agents, @vercel/agent-readability, Yoast llms.txt | These tools tidy or map your pages for AI. Rebilder answers from those pages and shows you who asked. |
| Intercom Fin, Tidio Lyro | Chatbots answer the customers who contact you. Rebilder answers the AI assistants that visit your site for them. |
| SEO on its own, Shopify agentic storefronts, Writing an llms.txt yourself | Good SEO, your store platform and a simple llms.txt all help. Here is what Rebilder adds to each. |

Source: [Compare](https://rebilder.com/compare)

## From nothing to deployed

Four commands and one setting. This puts the gateway in front of a site you already have, on a domain already on Cloudflare, on any plan including Free. It suits a site on a hosted platform, where you cannot run code on the origin.

Scaffold and install

```bash
npm create cloudflare@latest my-rebilder-gateway -- --type=hello-world --ts
cd my-rebilder-gateway
npm install @rebilder/gateway
```

Then replace `src/index.ts` with the worker below, set `pages: true` in its config so every page gets a copy made from the page your origin serves ([Automatic pages](/docs/pages)), and point `wrangler.jsonc` at your zone:

wrangler.jsonc: the only required edit

```json
"routes": [
  { "pattern": "example.com/*", "zone_name": "example.com" },
  { "pattern": "www.example.com/*", "zone_name": "example.com" }
]
```

Ship it

```bash
npx wrangler login
npx wrangler deploy
```

Verify with `curl -sI https://example.com/your-page -H "Accept: text/markdown"`: seeing `content-type: text/markdown` and `x-rebilder-path: markdown` means you are live. `npx wrangler tail` streams the events.

> **Rolling back takes one command** `npx wrangler delete` removes the worker and every route with it, and your site immediately serves exactly as it did before. The gateway never modified your origin, so there is nothing to unwind. To disable without deleting, comment out `routes` and redeploy.

## Hand this to your coding agent

If you would rather not wire it by hand, paste this into Claude Code, Cursor, or whatever you build with. It is written to be unambiguous about the parts that are easy to get wrong.

Prompt

```text
Set up a Cloudflare Worker that puts @rebilder/gateway in front of my site.

WHAT IT DOES
The Worker sits on a route in front of the origin. When an AI agent requests a
URL with `Accept: text/markdown`, it answers with a clean markdown copy of
that page, made from the page the origin serves. Everything else, humans and
Googlebot included, is forwarded to the origin unchanged.

STEPS
1. Scaffold a TypeScript Worker and add the dependency:
     npm create cloudflare@latest my-rebilder-gateway -- --type=hello-world --ts
     cd my-rebilder-gateway && npm install @rebilder/gateway
   The `pages` option in step 3 needs version 0.9.0 or later.
2. Read node_modules/@rebilder/gateway/README.md before writing anything. Do
   not work from memory of this prompt.
3. Create `src/gateway-config.ts` exporting a `GatewayConfig` with
   `pages: true`. The Worker then reads each page from the origin and serves
   its copy, so the whole site is covered before I describe anything.
4. For the pages whose copy should say more than the page does, create
   `src/content.ts`: a `Map<string, DocumentSource>` keyed by pathname, and a
   `Map<string, CollectionSource>` for index pages. Wire them as the `document`
   and `collection` resolvers (`Map.get` on the normalised pathname) with a
   pure synchronous `match` router returning 'document', 'collection', or null.
   Read real values from authoritative business sources. See the rules below.
5. Make `src/index.ts`:
     import { createGatewayFetchHandler } from '@rebilder/gateway/edge'
     export default { fetch: createGatewayFetchHandler(gatewayConfig) }
6. In wrangler.jsonc, set `routes` to my zone. Use the configured hostname; ask only if the deployment target is missing or ambiguous.

IMPLEMENTATION REQUIREMENTS
- Never invent a substantive value. Prices, opening hours, turnaround times,
  what a service includes, whether booking is required — read these from authoritative business sources. Ask only for unavailable facts needed for the task. Do not write a plausible-looking
  placeholder into content.ts; it will ship, and an agent will quote it.
- Money is in MINOR UNITS. { amount: 180, currency: 'GBP' } is £1.80, not £180.
  Use the currency’s minor-unit exponent when converting major units; preserve exact decimal arithmetic and reject unsupported precision.
- Serve the described pages from bundled or cached sources. If remote resolution is needed, bound its timeout, verify freshness and preserve the origin fallback.
- A resolver returns null when the URL is not that kind of page. null means
  "use the page's own copy", which is always a safe answer.
- Forward the origin's response as-is. Do not rewrite its status, do not turn a
  502 into a friendly page, do not follow its redirects inside the Worker.
- Do not change the origin. Preserve a rollback path that removes the Worker route and restores direct origin serving.

WHEN YOU ARE DONE
Tell me which pages answer with their own copy and which you described in
content.ts. Then verify against the deployed site, one real URL of each kind:

  curl -sI https://MY-DOMAIN/PATH -H 'Accept: text/markdown'   # expect text/markdown
  curl -sI https://MY-DOMAIN/PATH                              # expect my normal HTML
  npx rebilder check https://MY-DOMAIN/PATH                    # grades the page

If you cannot answer "which URLs now serve markdown", the setup is not done.
```

## A complete worker

The adapter needs no Cloudflare imports: `(req: Request) => Promise<Response>` is the whole contract, and `export default { fetch }` satisfies a CF Worker’s fetch handler without any workers-types coupling. Deploy it on a route in front of the store:

worker.ts

```ts
// worker.ts: deploy on a route in front of your site, e.g. example.com/*
import { createGatewayFetchHandler } from '@rebilder/gateway/edge'
import { gatewayConfig } from './gateway-config'

export default { fetch: createGatewayFetchHandler(gatewayConfig) }
```

That is a complete worker: agent traffic gets the markdown copy of the page; **everything else is `fetch(req)`-ed to the origin unchanged**, which is the standard CF Worker reverse-proxy pattern, so browsers and crawlers get the canonical HTML exactly as if the worker weren’t there.

## Serving the non-markdown path yourself

Pass `options.fallback: (req) => Response | Promise<Response>` to serve the non-markdown path yourself instead of proxying, which is useful for workers that are not fronting an origin, or for tests.

## Latency note

> **The one adapter with a network call** This is the one adapter where a network call happens: the origin pass-through on the **non-markdown** path, a round-trip that would occur without the worker anyway. A page answered by a source stays pure compute within the edge budget (p95 < 50ms). With `pages` on, the first request for a page reads it from your origin once, and later requests are answered from memory.

A gateway-internal error is contained like every other failure: the request falls through to fallback/origin, never a 500.

## Vercel Edge & other WinterCG runtimes

The same plain fetch handler works on Vercel Edge Functions (or any WinterCG runtime): export it as the handler. Use `options.fallback` there, since Vercel Edge functions don’t sit in front of an origin the way a route-mounted CF Worker does. In Next.js projects, prefer the [`./next` adapter](/docs/adapters/nextjs), which plugs into middleware natively.