Deploy your Astro Site to Netlify
Netlify offers hosting and serverless backend services for web applications and static websites. Any Astro site can be hosted on Netlify!
This guide includes instructions for deploying to Netlify through the website UI or Netlify’s CLI.
Project Configuration
Section titled Project ConfigurationYour Astro project can be deployed to Netlify in three different ways: as a static site, a server-rendered site, or an (experimental) edge-rendered site.
Static Site
Section titled Static SiteYour Astro project is a static site by default. You don’t need any extra configuration to deploy a static Astro site to Netlify.
Adapter for SSR/Edge
Section titled Adapter for SSR/EdgeTo enable SSR in your Astro project and deploy on Netlify:
Add the Netlify adapter to enable SSR in your Astro project with the following astro add
command. This will install the adapter and make the appropriate changes to your astro.config.mjs
file in one step.
If you prefer to install the adapter manually instead, complete the following two steps:
-
Install the
@astrojs/netlify
adapter to your project’s dependencies using your preferred package manager. If you’re using npm or aren’t sure, run this in the terminal: -
Add two new lines to your
astro.config.mjs
project configuration file.To render your project using Netlify’s experimental Edge Functions instead, change the
netlify/functions
import in the Astro config file to usenetlify/edge-functions
.
How to deploy
Section titled How to deployYou can deploy to Netlify through the website UI or using Netlify’s CLI (command line interface). The process is the same for both static and SSR Astro sites.
Website UI Deployment
Section titled Website UI DeploymentIf your project is stored in GitHub, GitLab, BitBucket, or Azure DevOps, you can use the Netlify website UI to deploy your Astro site.
-
Click Add a new site in your Netlify dashboard
-
Choose Import an existing project
When you import your Astro repository from your Git provider, Netlify should automatically detect and pre-fill the correct configuration settings for you.
-
Make sure that the following settings are entered, then press the Deploy button:
- Build Command:
astro build
ornpm run build
- Publish directory:
dist
- Build Command:
After deploying, you will be redirected to the site overview page. There, you can edit the details of your site.
Any future changes to your source repository will trigger preview and production deploys based on your deployment configuration.
netlify.toml
file
Section titled netlify.toml fileYou can optionally create a new netlify.toml
file at the top level of your project repository to configure your build command and publish directory, as well as other project settings including environment variables and redirects. Netlify will read this file and automatically configure your deployment.
To configure the default settings, create a netlify.toml
file with the following contents:
Using pnpm
on Netlify? Use the following settings instead:
📚 More info at “Deploying an existing Astro Git repository” on Netlify’s blog
CLI Deployment
Section titled CLI DeploymentYou can also create a new site on Netlify and link up your Git repository by installing and using the Netlify CLI.
-
Install Netlify’s CLI globally
-
Run the CLI and follow the instructions to log in and authorize Netlify
-
Run
netlify init
and follow the instructions -
Confirm your build command (
astro build
)The CLI will automatically detect the build settings (
astro build
) and deploy directory (dist
), and will offer to automatically generate anetlify.toml
file with those settings. -
Build and deploy by pushing to Git
The CLI will add a deploy key to the repository, which means your site will be automatically rebuilt on Netlify every time you
git push
.
📚 More details from Netlify on Deploy an Astro site using the Netlify CLI
Set a Node.js Version
Section titled Set a Node.js VersionIf you are using a legacy build image (Xenial) on Netlify, make sure that your Node.js version is set. Astro requires 14.15.0, v16.0.0, or higher.
You can specify your Node.js version in Netlify using:
- a
.nvmrc
file in your base directory. - a
NODE_VERSION
environment variable in your site’s settings using the Netlify project dashboard.
Using Netlify Functions
Section titled Using Netlify FunctionsNo special configuration is required to use Netlify Functions with Astro. Add a netlify/functions
directory to your project root and follow the Netlify Functions documentation to get started!
Examples
Section titled Examples- How to deploy an Astro site — Netlify Blog
- Deploy An Astro site with Forms, Serverless Functions, and Redirects — Netlify Blog
- Deployment Walkthrough Video — Netlify YouTube channel