🔥 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 Netlify: Static Sites, Forms and SSR

Deploy Astro to Netlify with netlify.toml or the CLI: build settings, Node version, redirects, headers, Netlify Forms on static pages and the Netlify adapter for SSR.

#Astro#Netlify#Deployment#Hosting#Forms

To deploy Astro to Netlify, import your Git repository in the Netlify dashboard; Netlify detects Astro and sets the build command to npm run build and the publish directory to dist. Click deploy, and every push to your main branch rebuilds the site while pull requests get deploy previews. A static Astro site needs no adapter.

Commit a netlify.toml so those settings live with the code, and you have a repeatable setup. This guide covers that file, deploying from the CLI, Netlify Forms on a static Astro site, redirects and headers, and the adapter for on-demand routes.

Key Takeaways

  • Netlify auto-detects Astro: build command npm run build, publish directory dist, no adapter for static output.
  • Pin Node.js 22 in netlify.toml or .nvmrc; Astro 7 requires 22.23.4 or newer.
  • Netlify Forms work on static Astro pages: add data-netlify="true" and a hidden form-name field.
  • Redirects and headers go in public/_redirects and public/_headers, or in netlify.toml.
  • npx astro add netlify adds the adapter for on-demand rendering, Server Islands and Actions.

Deploy Astro to Netlify from Git

  1. Push your project to GitHub, GitLab, Bitbucket or Azure DevOps.
  2. In Netlify, choose Add new project (or Add new site) and Import an existing project.
  3. Pick your Git provider and the repository.
  4. Netlify detects Astro and fills in:
    • Build command: npm run build
    • Publish directory: dist
  5. Add environment variables if the build needs them.
  6. Deploy.

The site goes live at a random *.netlify.app subdomain, which you can rename in the project settings. After that:

  • Pushes to the production branch deploy to production.
  • Each pull request gets a deploy preview with its own URL, and Netlify posts it on the PR.
  • You can enable branch deploys for long-lived branches like staging.

Put the settings in netlify.toml

Dashboard settings work, but a file in the repository is easier to review and reproduce:

# netlify.toml
[build]
  command = "npm run build"
  publish = "dist"

[build.environment]
  NODE_VERSION = "22"

Settings in netlify.toml override the dashboard. If the project is in a subfolder of a monorepo, add base = "apps/site" under [build] and paths become relative to it.

Astro 7 requires Node.js 22.23.4 or newer. Netlify’s build image picks a default Node version that changes over time, so pinning it avoids a surprise failure months later. An .nvmrc file containing 22 works too and also applies to local development.

Set site for Netlify

Set site to the production URL so the sitemap, RSS and canonical tags use it:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://www.example.com',
});

Deploy previews run the same build, so their canonicals point at production. That’s usually what you want, because it stops previews from being indexed as separate pages. If you need preview-specific URLs, Netlify exposes DEPLOY_PRIME_URL during the build, and you can read it in your config:

// astro.config.mjs
import { defineConfig } from 'astro/config';

const site = process.env.CONTEXT === 'production'
  ? 'https://www.example.com'
  : process.env.DEPLOY_PRIME_URL;

export default defineConfig({ site });

Deploy from the command line

The Netlify CLI deploys a local build without Git integration:

npm run build
npx netlify-cli deploy --dir=dist          # draft deploy with a preview URL
npx netlify-cli deploy --dir=dist --prod   # production

The first run asks you to log in and link or create a project. In CI, set NETLIFY_AUTH_TOKEN and NETLIFY_SITE_ID as secrets and the CLI runs without prompts.

Environment variables

Add them under Project configuration > Environment variables. Netlify lets you scope each variable to builds, functions or both, and give it different values per deploy context (production, deploy previews, branch deploys).

For a static Astro site, only build-time access matters. Variables prefixed PUBLIC_ and read with import.meta.env.PUBLIC_* are inlined into the output; others are readable during the build and never reach the browser. With an adapter, server routes can read runtime variables as well. The Astro environment variables guide explains the astro:env schema, which fails the build when a required variable is missing.

Netlify Forms on a static Astro site

Netlify Forms is a good reason to choose Netlify for a static site. Netlify scans your built HTML at deploy time, finds forms marked for it, and accepts submissions without any server code from you.

---
// src/pages/contact.astro
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="Contact" description="Send us a message.">
  <form name="contact" method="POST" data-netlify="true" netlify-honeypot="company" action="/thanks/">
    <input type="hidden" name="form-name" value="contact" />
    <p hidden>
      <label>Leave this empty: <input name="company" /></label>
    </p>
    <label>Name <input name="name" required /></label>
    <label>Email <input type="email" name="email" required /></label>
    <label>Message <textarea name="message" required></textarea></label>
    <button type="submit">Send</button>
  </form>
</BaseLayout>

The pieces:

  • data-netlify="true" tells Netlify to register the form. Netlify removes the attribute when it processes the HTML.
  • The hidden form-name input must match the form’s name. Astro renders forms as static HTML, so Netlify sees the form at deploy time, but including the field explicitly avoids problems if you later submit with JavaScript.
  • netlify-honeypot names a hidden field that bots fill and humans don’t, which filters basic spam.
  • action="/thanks/" sends people to your own confirmation page. Create src/pages/thanks.astro.

Submissions appear under Forms in the dashboard, and you can set email or webhook notifications. Netlify’s free plan includes a monthly submission allowance; check current limits on its pricing page. For other options on static sites, see the static contact forms guide.

Redirects and headers

Netlify reads _redirects and _headers from the publish directory. Put them in public/:

# public/_redirects
/old-blog/*   /blog/:splat   301
/home         /              301
/api/*        https://api.example.com/:splat  200

The last rule is a proxy (status 200): requests to /api/ are fetched from another origin without changing the URL. It’s handy for avoiding CORS when calling an API from a static page.

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

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

You can write the same rules in netlify.toml with [[redirects]] and [[headers]] blocks if you prefer one file:

# netlify.toml
[[redirects]]
  from = "/old-blog/*"
  to = "/blog/:splat"
  status = 301

Astro’s own redirects config option also works. On a static build it outputs meta-refresh HTML pages, while with the Netlify adapter it writes real Netlify redirect rules. For SEO-sensitive redirects on a static site, prefer _redirects, which returns a true 301.

404 pages

Add src/pages/404.astro. Astro builds it to dist/404.html, and Netlify serves it automatically for any path that doesn’t exist, with a 404 status. No configuration needed. The custom 404 page tip has a template with search and popular links.

On-demand rendering with the Netlify adapter

When some routes need to run per request, add the adapter:

npx astro add netlify
// astro.config.mjs
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
  site: 'https://www.example.com',
  adapter: netlify(),
});

Pages stay prerendered unless you opt them out with export const prerender = false, or set output: 'server' to make on-demand the default. On-demand routes run as Netlify Functions. The adapter also:

  • Uses Netlify’s Image CDN for astro:assets images on deployed sites, so images are transformed on request rather than at build time.
  • Lets you run Astro middleware at the edge with the edgeMiddleware option.
  • Supports caching on-demand pages on Netlify’s CDN with standard Cache-Control headers you set in the route.

Check the Astro Netlify adapter docs for the current option list, since it changes between major versions.

Troubleshooting

Symptom Likely cause Fix
“Page not found” for the whole site Publish directory isn’t dist Set publish = "dist"
Build fails with a Node error Old default Node NODE_VERSION = "22" in netlify.toml
Form submissions don’t appear Form not in built HTML, or form-name mismatch Check dist/ for the form; match the names
Canonicals point at localhost site not set Set site and rebuild
Old content after deploy Browser cache on HTML Don’t set long Cache-Control on HTML

To undo a bad release, open Deploys, select an earlier deploy and choose Publish deploy. It goes live instantly without a rebuild.

Netlify compared with Cloudflare Pages and Vercel

For a static Astro site, all three work almost identically. Netlify’s edge is built-in form handling, proxy rewrites in _redirects, and a mature dashboard. Cloudflare has the larger network and its own storage products; Vercel suits teams already on it. The Astro deployment guide compares them in one table, and there’s a matching Cloudflare Pages walkthrough.

Komal themes are static Astro 7 projects, so they deploy to Netlify with the netlify.toml above and nothing else. The static hosting docs cover Netlify for a theme, and you can browse Astro themes to pick one to deploy.

FAQ

Is Netlify free for Astro sites?

Netlify has a free plan that covers most personal and small business Astro sites, with monthly limits on bandwidth, build minutes and form submissions. Check Netlify’s pricing page for the current numbers.

Do I need @astrojs/netlify to deploy Astro on Netlify?

No. Static Astro sites deploy without it. Install the adapter only when you need on-demand rendering, Server Islands, Actions or sessions.

What is the publish directory for Astro on Netlify?

dist, which is Astro’s default outDir. Netlify fills it in automatically when it detects Astro.

How do I use Netlify Forms with Astro?

Add data-netlify="true" and a name to the form, include a hidden form-name input with the same name, and deploy. Netlify finds the form in the built HTML and starts collecting submissions.

Why is my Astro build failing on Netlify but not locally?

The most common causes are an older Node.js version on Netlify, a missing lockfile, or an environment variable set locally but not in Netlify. Pin NODE_VERSION, commit the lockfile, and copy the variables to the project settings.