Creating your Pages project

Go to the Cloudflare dashboard, select Workers & Pages, and create a new Pages project. Connect it to your GitHub repository. Cloudflare will auto-detect Astro and suggest build settings, but verify: build command pnpm build, output directory dist, and Node.js version matching your engines field in package.json.

Environment variables

Cloudflare Pages has two scopes for environment variables: Production and Preview. Add SANITY_PROJECT_ID, SANITY_DATASET, SANITY_API_TOKEN, and PUBLIC_GTM_ID under Settings → Environment variables. Variables prefixed with PUBLIC_ are inlined into the static build — only use this prefix for values safe to expose publicly.

Deploy hooks

A deploy hook is a secret URL that triggers a new build when requested via HTTP POST. Create one under Pages → Settings → Builds & Deployments → Add deploy hook. This URL is what your Cloudflare Worker will call when Sanity publishes new content.

The batching worker

Without a batching layer, every Sanity publish triggers a full Pages rebuild. If an editor updates ten pages in quick succession, you queue ten redundant builds. The Cloudflare Worker in this stack receives webhook POSTs from Sanity and sets a KV flag. A cron trigger runs every minute: if the flag is set, it triggers one deploy and clears the flag — collapsing any number of publishes into a single build.