🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Deploy Astro to Cloudflare Pages: Step-by-Step Guide

Deploy Astro to Cloudflare Pages from Git or Wrangler: build settings, Node version, _headers and _redirects, 404 handling, preview URLs and custom domains.

#Astro#Cloudflare Pages#Deployment#Hosting#Wrangler

To deploy Astro to Cloudflare Pages, push the project to GitHub or GitLab, create a Pages project in the Cloudflare dashboard, pick the Astro preset (build command npm run build, output directory dist) and deploy. Every push to your production branch then rebuilds the site, and every other branch gets its own preview URL. A static Astro site needs no adapter.

This guide walks through the Git setup, deploying from the command line with Wrangler, the files Cloudflare reads from public/, and the problems people most often hit.

Key Takeaways

  • Static Astro sites deploy to Cloudflare Pages with no adapter: build command npm run build, output dist.
  • Astro 7 needs Node.js 22.23.4 or newer. Set NODE_VERSION or add a .nvmrc if the build fails on an older Node.
  • Put _headers and _redirects in public/; Astro copies them to dist/ and Pages applies them.
  • Without a 404.html, Pages treats the site as a single-page app and serves the home page for missing URLs. Add src/pages/404.astro.
  • For on-demand rendering, add @astrojs/cloudflare. Cloudflare now points new full-stack projects toward Workers.

Before you deploy

Check three things locally first. They cause most failed first deploys.

  1. The build passes. Run npm run build and confirm dist/ contains index.html.
  2. site is set. Sitemaps, canonical URLs and RSS need it:
// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://www.example.com',
});
  1. The lockfile is committed. Cloudflare installs dependencies from package-lock.json, pnpm-lock.yaml, yarn.lock or bun.lockb, and picks the package manager from whichever is present. A missing lockfile means different versions than you tested.

If you don’t have a custom domain yet, use the *.pages.dev address for site and change it later.

Deploy Astro to Cloudflare Pages from Git

  1. Push your project to a GitHub or GitLab repository.
  2. In the Cloudflare dashboard, open Workers & Pages and create a new application. Choose Pages, then Connect to Git.
  3. Authorize Cloudflare to read the repository and select it.
  4. Under build settings, choose the Astro framework preset. It fills in:
    • Build command: npm run build
    • Build output directory: dist
  5. If the project lives in a subfolder of the repository (a monorepo), set Root directory to that folder.
  6. Add environment variables if your build needs any (see below).
  7. Select Save and Deploy.

The first build takes a minute or two. When it finishes, the site is live at <project-name>.pages.dev.

From then on:

  • A push to the production branch (usually main) deploys to production.
  • A push to any other branch creates a preview deployment at <branch>.<project-name>.pages.dev, plus a unique URL per commit.
  • Pull requests get a comment with the preview link when the GitHub integration is installed.

Set the Node.js version

Astro 7 requires Node.js 22.23.4 or newer. If the Cloudflare build image defaults to an older version, the build fails during npm install or at the first astro command.

Two ways to pin it:

# .nvmrc (committed to the repository root)
22

Or add an environment variable in the Pages project settings: NODE_VERSION = 22. The file is better, because it also tells your teammates and any other CI what version to use.

Environment variables

Set them under Settings > Variables and Secrets for the Pages project, separately for production and preview environments.

For a static site, variables are only available at build time. Astro inlines public values that you reference with import.meta.env.PUBLIC_* into the HTML and JavaScript. Anything without the PUBLIC_ prefix can be read during the build but never reaches the browser.

If you use astro:env for typed variables, the schema in astro.config.mjs validates them during the build, so a missing variable fails the deploy instead of shipping a broken page. The environment variables guide covers both approaches.

Deploy from the command line with Wrangler

You don’t have to connect Git. Wrangler, Cloudflare’s CLI, uploads a local build directly:

npm run build
npx wrangler pages deploy dist --project-name my-astro-site

The first run asks you to log in and creates the project if it doesn’t exist. Add --branch staging to create a preview deployment instead of production.

This is useful when you build in your own CI (GitHub Actions, GitLab CI) and want Cloudflare only to host the output:

# .github/workflows/deploy.yml
name: Deploy to Cloudflare Pages
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run build
      - run: npx wrangler pages deploy dist --project-name my-astro-site
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

Create the API token in the Cloudflare dashboard with the Cloudflare Pages: Edit permission.

Headers and redirects

Cloudflare Pages reads two plain-text files from the root of the output directory. In Astro, put them in public/ and they’re copied to dist/ unchanged.

# public/_headers
/*
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin

/_astro/*
  Cache-Control: public, max-age=31536000, immutable

Everything in /_astro/ has a content hash in its filename, so it can be cached for a year. HTML should stay revalidated, which is the Pages default.

# public/_redirects
/old-blog/*   /blog/:splat   301
/pricing-2025 /pricing/      301
/docs         /docs/getting-started/ 302

You can also define redirects in astro.config.mjs with the redirects option. On a static build, Astro generates a small HTML page with a meta refresh for each one. A _redirects rule is better for SEO because Cloudflare returns a real 301 status. The Astro redirects tip compares both.

404 pages and trailing slashes

Pages has one behavior that surprises people: if there’s no 404.html at the top level of the output, it assumes you deployed a single-page app and serves index.html for every unknown path, with a 200 status. Search engines then see thousands of “pages” that are all your home page.

The fix is to add src/pages/404.astro. Astro builds it to dist/404.html, and Pages serves it with a 404 status for missing URLs. The custom 404 page tip has a template.

For trailing slashes, Pages serves about/index.html at /about/ and redirects /about to it. That matches Astro’s default build.format: 'directory'. If you set trailingSlash: 'always' in Astro, links and canonicals agree with what Pages serves.

Custom domains

In the Pages project, open Custom domains and add your domain. If the domain’s DNS is already on Cloudflare, the record is created for you. If it’s elsewhere, add the CNAME record Cloudflare shows (for subdomains like www) or move the domain’s nameservers to Cloudflare (needed for an apex domain like example.com).

HTTPS certificates are issued automatically. After the domain is active, update site in astro.config.mjs to match and redeploy, so canonicals and the sitemap use the real domain. The custom domain tip covers DNS for common registrars.

On-demand rendering on Cloudflare

Everything above is for static output. If some routes need to run per request (export const prerender = false, Server Islands, Actions, sessions), add the Cloudflare adapter:

npx astro add cloudflare

That installs @astrojs/cloudflare and sets it as the adapter in your config. Prerendered pages are still served as static assets; only on-demand routes run as code on Cloudflare’s network.

Cloudflare now steers new full-stack projects toward Workers (with static assets) rather than Pages Functions, and current versions of the adapter document the Workers setup first. For a purely static site the choice doesn’t matter much; Pages remains the simplest path. If you’re adding server code, read the Astro Cloudflare deploy guide for the setup that matches your adapter version, and Cloudflare’s own Astro framework guide.

Troubleshooting

Symptom Likely cause Fix
Build fails with an engine or syntax error Old Node.js Add .nvmrc with 22
Build passes, site shows a 404 everywhere Wrong output directory Set it to dist
Missing URLs show the home page No 404.html Add src/pages/404.astro
Sitemap or canonicals use localhost site not set Set site and rebuild
Styles missing on a subpath base set but links hard-coded Use import.meta.env.BASE_URL in links
_headers rules ignored File not in public/ Move it to public/_headers

If a deploy breaks production, open Deployments, pick the last good one and choose Rollback. Pages keeps previous deployments, so this takes seconds and doesn’t need a rebuild.

Cloudflare Pages compared with other hosts

Netlify and Vercel offer nearly the same workflow for static Astro sites. Cloudflare’s strengths are its large global network and easy access to Workers, KV, R2 and D1 if you add server features later. The Astro deployment guide compares all the common hosts side by side, and there are dedicated walkthroughs for Netlify and Vercel.

Komal themes are static Astro 7 projects, so they deploy to Pages with exactly these settings and no adapter. The Cloudflare deploy docs walk through it for a theme, and you can browse Astro themes if you want a finished site to deploy.

FAQ

Is Cloudflare Pages free for Astro sites?

Cloudflare Pages has a free plan that covers most static Astro sites, with limits on monthly builds and files per site. Check Cloudflare’s current pricing page for the exact limits before launching a large site.

Do I need the Cloudflare adapter to deploy Astro?

No, not for a static site. The adapter is only needed when some routes render on demand, for example with prerender = false, Server Islands or Actions.

What is the build output directory for Astro on Cloudflare Pages?

dist. That’s where astro build writes the site unless you changed outDir in astro.config.mjs.

The build has no 404.html, so Pages treats the site as a single-page app. Add src/pages/404.astro and redeploy; Pages will then return a real 404.

How do I deploy Astro to Cloudflare Pages without GitHub?

Build locally with npm run build, then run npx wrangler pages deploy dist --project-name your-project. Wrangler uploads the folder directly.