🔥 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

Astro Preview Deploy for Every Pull Request

Set up an Astro preview deploy for every pull request on Cloudflare Pages, Netlify or Vercel, keep previews out of search with noindex, and test them before merging.

#Astro#Deployment#Preview Deployments#CI

An Astro preview deploy is a temporary copy of the site built from a pull request, with its own URL, so you can review changes before they reach production. Cloudflare Pages, Netlify and Vercel create one automatically for every pull request once the repository is connected. Your job is to keep previews out of search results and make sure their canonical URLs still point at production.

Key Takeaways

  • Connecting a Git repository to Cloudflare Pages, Netlify or Vercel gives you pull request previews with no extra config.
  • Keep site set to production so canonicals on previews point at the real pages.
  • Add noindex on previews using the host’s environment variables.
  • Run Playwright or Lighthouse against the preview URL before merging.

Turn previews on

Host How previews work Env var that tells you it is a preview
Cloudflare Pages Every non-production branch gets a <branch>.<project>.pages.dev URL CF_PAGES_BRANCH (compare with your production branch)
Netlify Deploy Previews for each pull request CONTEXT is deploy-preview
Vercel Preview deployment for each push to a non-production branch VERCEL_ENV is preview

All three post the preview URL back to the pull request. Our guides for Cloudflare Pages, Netlify and Vercel cover the initial connection.

Some hosts add an X-Robots-Tag: noindex header to preview URLs by default, but do not rely on it; check with curl -sI <preview-url>. A meta tag set at build time works everywhere:

---
// src/components/Head.astro
const isPreview =
  process.env.CONTEXT === 'deploy-preview' ||
  process.env.VERCEL_ENV === 'preview' ||
  (process.env.CF_PAGES_BRANCH !== undefined && process.env.CF_PAGES_BRANCH !== 'main');

const canonical = new URL(Astro.url.pathname, Astro.site);
---
{isPreview && <meta name="robots" content="noindex, nofollow" />}
<link rel="canonical" href={canonical} />

This runs at build time on the host, where those variables exist, so production builds never include the tag. Change 'main' to your production branch name. Keeping site pointed at production means every preview page canonicalises to the live URL, which is what you want. Our canonical URLs tip and site option tip explain why.

For a cleaner setup, define a typed variable with astro:env and set it only in the preview environment in your host’s dashboard. The environment variables guide shows how.

Previews from your own CI

If you deploy with GitHub Actions, Wrangler can push a preview branch to Cloudflare Pages:

# .github/workflows/preview.yml
name: preview
on: pull_request
jobs:
  preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - run: npm ci
      - run: npm run build
      - run: npx wrangler pages deploy dist --project-name=my-astro-site --branch=${{ github.head_ref }}
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

Current Astro releases need Node.js 22.23.4 or newer, which node-version: '22' provides. In a custom pipeline the host’s preview variables may not be set during the build, so pass your own, for example PREVIEW=true, and check that in Head.astro. Firebase preview channels and Render pull request previews work similarly; see our Firebase tip.

Test the preview, not just the code

A preview is the best place to run checks that need a real deployment:

  1. End-to-end tests. Point Playwright’s baseURL at the preview URL instead of a local server; see the Playwright tip.
  2. Performance. Run Lighthouse on two or three key pages.
  3. Headers and redirects. _headers, _redirects and vercel.json only take effect on the host.
  4. Content review. Share the URL with whoever approved the copy.

Themes from our Astro theme catalog are standard Astro projects, so previews work on all three hosts without changes. The Cloudflare deploy docs cover the production setup.

FAQ

How do I get preview deployments for an Astro site?

Connect the repository to Cloudflare Pages, Netlify or Vercel. Each creates a preview deployment with its own URL for pull requests or non-production branches automatically.

Will preview deployments hurt my Astro site’s SEO?

Not if you add noindex on previews and keep canonicals pointing at production. Build the meta tag from the host’s preview environment variable, as shown above.

Should site in astro.config.mjs change for previews?

Usually not. Keeping site set to production makes every preview canonicalise to the live page. Only override it if you need to test absolute URLs such as Open Graph images on the preview.

Can I run tests against an Astro preview deploy?

Yes. Use the preview URL as Playwright’s baseURL or as the target for Lighthouse in CI, then merge only when they pass.