Deploying Astro to Cloudflare Pages
Build settings for Cloudflare Pages, preview noindex, a custom domain with the pages.dev address redirected to it, and response headers.
The starter builds a fully static site, so Cloudflare Pages can serve it straight from the build output with no adapter and no server code. This page covers the settings that matter for search: the production URL in the build, previews that stay out of search results, one address per page and the response headers worth adding.
Build settings
In the Cloudflare dashboard, create a Pages project and connect your repository. Use these settings, with your own domain in place of the example:
- Production branch
main- Build command
bun run build --site https://example.com && bun run verify:output && bun run lint:html- Build output directory
dist- Environment variables
NODE_VERSIONset to24.8.0andBUN_VERSIONset to1.4.5, matchingpackageManagerinpackage.json
--site gives the build its production origin without writing it into the source, which keeps a fresh fork from publishing canonical URLs that point at someone else's domain. If you set site inastro.config.mjs instead, the command can start withbun run build. The two checks after the build read the record it leaves behind; if either fails, the deployment fails and the previous one stays live. The build checks guide explains what they look for.
Preview deployments
Every branch and pull request gets its own preview address underpages.dev. Cloudflare already sendsX-Robots-Tag: noindex for requests to preview addresses. To mark the pages themselves as well, add SITE_NOINDEX with the value true to the preview environment's variables and leave it unset for production. Keep the production origin in the preview build command: canonical URLs then point at the real site, so a preview that does get crawled credits production rather than competing with it.
A custom domain, and one address per page
Add your domain under the project's custom domains; thesite value only controls generated URLs and does not touch DNS. Once the domain works, the same pages are still reachable atyour-project.pages.dev. The canonical links already name your domain, but a redirect is cleaner. Cloudflare does this with Bulk Redirects: create a list with your-project.pages.dev as the source, https://example.com as the target and status301, tick preserve query string, subpath matching and preserve path suffix, then create a Bulk Redirect rule that uses the list. Include subdomains also redirects the preview addresses underyour-project.pages.dev, so leave it unticked if you review previews there.
Within your domain, Pages serves about/index.html at/about/ and redirects both /about and/about/index.html there. That matches the starter'strailingSlash: 'always', so canonical links, the sitemap and the served URL are the same address.
The 404 page
Keep src/pages/404.astro. Pages serves a top-level404.html with a real 404 status for unknown paths. Without one, Pages treats the project as a single-page application and answers every unknown path with the homepage and a 200 status, which search engines report as soft 404s.
Response headers
Pages sends Cache-Control: public, max-age=0, must-revalidatewith an ETag by default, so browsers revalidate each file. That suits the starter: title cards, icons and pages keep the same URL when their content changes. To add headers, createpublic/_headers; Astro copies it into the output. This example keeps the pages.dev address out of search results if you skip the redirect, and caches the fingerprinted files Astro writes to /_astro/ once you add styles or scripts:
https://:project.pages.dev/*
X-Robots-Tag: noindex
/_astro/*
Cache-Control: public, max-age=31536000, immutableA _headers file takes up to 100 rules with lines of up to 2,000 characters. It applies to static files only, not to responses from Pages Functions. Do not mark title cards or icons as immutable; they would stay stale in caches after you change them.
After the first deployment
Open /robots.txt and /sitemap-index.xml on your domain and check that they name it. View the source of a page and check its canonical link. Then add the domain to Google Search Console and Bing Webmaster Tools and submit the sitemap index in both.