Many old guides for deploying Astro on Cloudflare no longer work. They talk about Pages, Astro.locals.runtime and a _worker.js folder, and all three are gone from the current adapter. This post is for anyone who has an Astro site and wants to run it on Cloudflare today, either as a fully static site or with some routes rendered on demand.
I use Astro 7.4.13, @astrojs/cloudflare 14.4.9 and Wrangler 4.169.1. The example has a static home page, an on-demand page that reads a KV namespace, an API route that reads a variable and a secret, and a custom 404 page.
Workers or Pages?
Use Workers. The Astro deploy guide recommends Cloudflare Workers for new projects, and the Cloudflare adapter dropped Pages support in v13. If you have an existing Pages project, Cloudflare has a migration guide.
Workers now serve static assets too, so you don’t need Pages to host a static site. Workers also get features Pages doesn’t have: the Vite plugin, Workers Logs, Cron Triggers and gradual deployments. There is one limitation to check before you move: a Worker custom domain must be a zone managed by Cloudflare, while Pages also accepts domains whose DNS is hosted elsewhere.
Static: you don’t need the adapter
If every page is prerendered, you don’t need the adapter at all. astro build outputs HTML to dist/, and a small Wrangler config tells Cloudflare where to find it:
{
"name": "my-astro-site",
"compatibility_date": "2026-11-30",
"assets": {
"directory": "./dist",
"not_found_handling": "404-page"
}
}
With "404-page", Cloudflare serves the nearest 404.html for missing paths. Without it, the default is "none", which returns an empty 404. Then deploy:
npx astro build && npx wrangler deploy
No Worker code runs: Cloudflare serves the files directly.
On demand: add the adapter
As soon as you want a route rendered per request, a server island, actions or sessions, add the adapter:
npx astro add cloudflare
The command installs @astrojs/cloudflare, adds it to astro.config.mjs and creates a wrangler.jsonc. My config looks like this:
// astro.config.mjs
import { defineConfig, envField } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';
export default defineConfig({
adapter: cloudflare({
imageService: 'compile',
}),
session: false,
env: {
schema: {
GREETING: envField.string({ context: 'server', access: 'public', default: 'Hello' }),
API_TOKEN: envField.string({ context: 'server', access: 'secret' }),
},
},
});
I left output at the default 'static'. Every page is prerendered unless it opts out with export const prerender = false. For a content site where only a few routes need a server, I think this is the right default. If most of your pages are dynamic, set output: 'server' and opt out the other way with prerender = true.
Two options in the config are worth explaining:
session: false: the adapter sets up Astro sessions on Workers KV by default, with a binding calledSESSION. I don’t use sessions here, so I turned them off and that binding is gone.imageService: 'compile': the default is'cloudflare-binding', which uses the Cloudflare Images binding.'compile'optimizes images at build time for prerendered pages and passes them through unchanged on on-demand pages. For a mostly static site, that is all I need.
Since v13 the adapter builds prerendered pages inside workerd, the Workers runtime, and not in Node. If a static page reads files with node:fs at build time, set prerenderEnvironment: 'node'.
wrangler.jsonc
This is the file that controls the Worker:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "deploy-astro-on-cloudflare",
"main": "@astrojs/cloudflare/entrypoints/server",
"compatibility_date": "2026-11-30",
"compatibility_flags": ["nodejs_compat"],
"assets": {
"not_found_handling": "404-page"
},
"observability": { "enabled": true },
"vars": {
"GREETING": "Ciao"
},
"kv_namespaces": [
{ "binding": "VISITS" }
]
}
mainpoints to the adapter’s entrypoint, not to a file indist/. Older configs useddist/_worker.js/index.js, which you can now delete.- In
assetsI only set the 404 behaviour. At build time the adapter adds the output directory and theASSETSbinding. You can see the result indist/server/wrangler.json, and that generated file is whatwrangler deployreads. compatibility_datefixes the runtime behaviour your Worker gets. Set it to the day you create the project and update it on purpose, not by accident.- The
VISITSnamespace has noid. Wrangler creates the resource on the first deploy and, when you deploy from the CLI, writes the id back into the file. Deploys from Git don’t write it back, so commit the id once it exists.
The config file is optional: without it, Astro generates a default from the package.json name. I keep one anyway, because I need it for bindings.
Variables, secrets and bindings
Cloudflare has three kinds of runtime values, and each one is configured differently:
| What | Where it lives | Local dev |
|---|---|---|
| Plain variable | vars in wrangler.jsonc |
same file |
| Secret | wrangler secret put NAME or dashboard |
.dev.vars (gitignored) |
| Binding (KV, D1, R2…) | wrangler.jsonc |
simulated in .wrangler/ |
Astro.locals.runtime.env is gone. Bindings come from the cloudflare:workers module:
---
// src/pages/visits.astro
export const prerender = false;
import { env } from 'cloudflare:workers';
import { GREETING } from 'astro:env/server';
const current = Number((await env.VISITS.get('count')) ?? '0') + 1;
await env.VISITS.put('count', String(current));
---
<h1>{GREETING}!</h1>
<p>This page has been rendered <strong>{current}</strong> times.</p>
For variables and secrets I use astro:env. The schema in astro.config.mjs gives me types, a default for GREETING and an error if API_TOKEN is missing. In the API route, the secret never leaves the server:
// src/pages/api/info.ts
import type { APIRoute } from 'astro';
import { API_TOKEN, GREETING } from 'astro:env/server';
export const prerender = false;
export const GET: APIRoute = async ({ locals }) => {
locals.cfContext.waitUntil(Promise.resolve(console.log('info requested')));
return Response.json({ greeting: GREETING, hasToken: API_TOKEN.length > 0 });
};
locals.cfContext replaces the old runtime.ctx. Use it for waitUntil(). Request metadata such as the country is in Astro.request.cf.
To type env.VISITS, run wrangler types. It reads wrangler.jsonc and generates worker-configuration.d.ts. I run it before dev, build and check, so the types never go stale:
{
"scripts": {
"dev": "wrangler types && astro dev",
"build": "wrangler types && astro build",
"preview": "astro build && astro preview",
"deploy": "astro build && wrangler deploy"
}
}
One limitation: KV is eventually consistent, so a write can take up to 60 seconds to reach every region. A visit counter can live with that. For anything that needs exact counts, use D1 or a Durable Object.
Local development
astro dev already runs your code in workerd through the Cloudflare Vite plugin. As a result, cloudflare:workers, KV and secrets from .dev.vars work locally without a separate wrangler dev. astro preview serves the production build in the same runtime. Running both was the quickest way for me to check that the static page, the counter and the 404 page behaved as they would in production.
Deploy from the CLI
npx wrangler login
npx wrangler secret put API_TOKEN
npm run deploy
Wrangler prints the *.workers.dev URL at the end. Before the real deploy, npx wrangler deploy --dry-run is a useful check: it lists the files it would upload and the bindings the Worker will get.
If you use Wrangler environments (staging, production), note that the environment is now chosen at build time. Each environment needs its own build:
CLOUDFLARE_ENV=staging astro build && wrangler deploy
Deploy from Git
For automatic deploys, connect the repository from the dashboard: Workers & Pages → Create application → Import a repository. Set the build command to npm run build (or npx astro build) and the deploy command to npx wrangler deploy. Every push to the production branch triggers a deploy. Add the secrets in the Worker’s settings, because .dev.vars is never committed.
What I verified and what I didn’t
The example builds, passes astro check, and works in astro dev and astro preview. wrangler deploy --dry-run lists the expected bindings. I did not run a real deploy to a Cloudflare account or set up the Git integration for this post. The steps above follow the official docs, and the README in the example says the same.
Conclusion
My rule is simple. If the site is fully static, skip the adapter: a short wrangler.jsonc and wrangler deploy are enough. Add @astrojs/cloudflare only when a route needs to run per request, keep output: 'static', and opt in page by page. You get two things in return: a dev server that runs in the same runtime as production, and direct access to KV, D1 and the other bindings. What you give up is Node: some npm packages and node: APIs won’t work, even with nodejs_compat. Check your dependencies before you commit to it.
