
Cloudflare is the best place we know to host an Astro site: fast everywhere, simple to deploy and, for most marketing sites, close to free. This guide covers the set-up we use, with the files you need: hosting, redirects, caching, forms and rebuilding when content changes.
Why Cloudflare
- Speed. Pages are served from Cloudflare's network close to your visitors.
- Cost. On the Workers platform, requests for static files are free and unlimited, and storing them costs nothing extra.
- One place. DNS, security, hosting, redirects and small server functions in one account.
- Astro's home. Cloudflare acquired the Astro team in January 2026. Astro stays open source and runs on other hosts too, but the two are designed to work well together.
The basic set-up
Build Astro as a static site, then tell Cloudflare where the files are with a small config file:
{
"name": "acme-site",
"compatibility_date": "2026-11-01",
"assets": {
"directory": "./dist",
"not_found_handling": "404-page"
}
}Connect the GitHub repository in the Cloudflare dashboard with Workers Builds, set the build command to npm run build and the deploy command to npx wrangler deploy. Every push to the main branch now builds and publishes the site.
Redirects
Cloudflare reads a plain text _redirects file from your build output. In Astro, put it in the public folder. It takes up to 2,000 fixed redirects and 100 pattern rules, which covers almost any marketing site.
# Fixed redirects first
/old-pricing /pricing 301
/legal /legals/privacy 301
# Pattern rules after
/blog/:slug /articles/:slug 301
/news/* /articles/:splat 301Keep fixed redirects above pattern rules. If redirects are managed in your CMS, generate this file during the build instead of editing it by hand.
Caching and security headers
A _headers file sets headers for matching paths. Astro gives built files unique names, so they can be cached for a year:
/_astro/*
Cache-Control: public, max-age=31536000, immutable
/*
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Strict-Transport-Security: max-age=31536000Forms without a third-party tool
A small Worker can handle your contact form while everything else stays static. This one saves each message to a D1 database as a backup, then sends the visitor to a thank-you page:
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === '/api/contact' && request.method === 'POST') {
const form = await request.formData();
await env.FORMS.prepare('INSERT INTO submissions (name, email, message) VALUES (?, ?, ?)')
.bind(form.get('name'), form.get('email'), form.get('message'))
.run();
// Send the email notification here with your email provider.
return Response.redirect(new URL('/thanks', url), 303);
}
return env.ASSETS.fetch(request);
},
};{
"name": "acme-site",
"main": "worker/index.js",
"compatibility_date": "2026-11-01",
"assets": { "directory": "./dist", "binding": "ASSETS", "run_worker_first": ["/api/*"] },
"d1_databases": [{ "binding": "FORMS", "database_name": "acme-forms", "database_id": "your-database-id" }]
}Only requests to /api/ run the Worker. Everything else is served as static files, so it stays free. The Workers free plan includes 100,000 Worker requests a day, far more than a contact form needs. Add validation and spam protection before you go live.
Rebuilding when content changes
A static site needs a rebuild when an editor publishes. Create a deploy hook in Workers Builds, then add a webhook in Sanity that calls it whenever a document is published. Keep the hook URL secret: anyone with it can trigger a build. On our own site, a published change is live in about a minute.
Limits worth knowing
- Files: 20,000 static files per deployment on the free plan, 100,000 on paid. Plenty for most sites.
- Redirects: 2,000 fixed and 100 pattern rules in _redirects. Beyond that, use Cloudflare's Bulk Redirects.
- Worker requests: 100,000 a day on the free plan, unlimited for static files.
Checklist
- wrangler.jsonc pointing at the build folder
- GitHub connected with Workers Builds
- _redirects for every old address
- _headers for caching and security
- A Worker for forms, if you need one
- A deploy hook called by your CMS on publish
- Your custom domain added, with the SSL certificate active
Want it set up for you, with an AI assistant that can manage it safely? Read the AI-ready website guide or see our Astro and Sanity development service.