Documentation menu

Cloudflare Worker & edge

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

A complete worker

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
// worker.ts — deploy on a route in front of the store, e.g. store.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 with a matching source gets the standard markdown response; everything else is fetch(req)-ed to the origin unchanged — 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 — for workers that are not fronting an origin, or for tests.

One honest latency note

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, which plugs into middleware natively.