How to deploy an Astro site
Tags: webdev, javascript, jamstack, astro
Published: 2022-06-14 08:30:00

Astro is a new kind of static site generator for the *modern web*, and is centered around helping you ship less client-side JavaScript in your projects. When you combine Astro with Netlify — *the platform for modern web development* — deploying your new Astro site is a seamless and enjoyable experience! In this tutorial, we'll take a look at the different ways you can deploy a new Astro site on Netlify, and the choices you need to make along the way. But first, a little overview of Astro.

## An overview of Astro

[Astro](https://astro.build) is currently in beta (version 1.0.0 at the time of writing), and was launched to the web development community in early 2021. In an Astro project, you write JavaScript, HTML and CSS in `.astro` files, and the extra special thing about Astro is that you can also build projects using your favorite JavaScript front end frameworks. Configure your projects to use React, Preact, Svelte, Vue, SolidJS, AlpineJS and Lit by [installing integrations via the Astro config file](https://docs.astro.build/en/core-concepts/framework-components/), and you’re good to go.

## Starting a new Astro project

There are a number of ways you can spin up a new Astro project depending on your preferences. Let’s take a look at:

- [how to start an Astro project using the Astro CLI](#using-the-astro-cli)
- [how to get started with pre-configured in-browser environments](#using-in-browser-environments)
- [how to deploy Netlify templates](#deploy-an-astro-template-to-netlify)

### Using the Astro CLI

Open up your terminal, and run the following command depending on your preferred package manager.

```bash
# create a new project with npm
npm create astro@latest

# or yarn
yarn create astro

# or pnpm
pnpm create astro@latest
```

The Astro CLI will guide you through setting up your new project, including prompting you to select one of the pre-configured templates.

![A terminal showing that npm create astro@latest has been run. The cursor has selected the template named Just the basics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqx9yr0w3ohj26pegcl6.png)

Next, the CLI will ask if you want to install dependencies (select yes if you want to run a development server on your machine), and whether you’d like to add support for component frameworks. Don’t worry if you’re not ready to add component frameworks just yet — you can do it later. Here’s how a finished installation looks in the terminal with the Astro CLI. At this stage, you’re already ready to deploy!

![A terminal window showing a freshly created Astro project using the Astro CLI. The latest instructions are to cd into the new project directory and run npm run dev.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prklz73nx9mxdistb9wq.png)

### Using in-browser environments

Astro offers you lots of ways to get started with a template project using browser-based development environments such as StackBlitz, CodeSandbox and GitPod. Head on over to [astro.new](https://astro.new/), and get started with an empty project or a template that uses a component framework. Clicking on the buttons for your preferred environment will open up the code repository in the same window.

![The astro.new page showing a selection of new projects to open in either StackBlitz, CodeSandbox or GitPod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f47penii53n2y4dtyqjg.png)

You can connect your browser environment to GitHub to fork the repository to your GitHub account when you’re ready to deploy your Astro site. Here’s the “Just the basics” template in CodeSandbox.

![The Astro Just the basics project opened in CodeSandbox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4zfr2xplsorln53ldnm.png)

### Deploy an Astro Template to Netlify

If you’d like to skip the steps above and try out Astro on Netlify in **just two minutes ✨**, we built an Astro quick start template you can deploy to Netlify. [Check out this blog post to learn more](https://www.netlify.com/blog/deploy-your-astro-project-fast/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=deploy-astro-fast), or click the Deploy to Netlify button below to try it out.

[![Deploy to Netlify Button](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify-templates/astro-quickstart)

The great thing about the template deploy method is that Netlify automatically creates a new repository in your GitHub account for you, kicks off a new build, runs tests, and assigns you a live URL — all in around two minutes.

Now you've created a new Astro site either with the Astro CLI or an in-browser environment, it's time to deploy! But first, there are some choices you need to make about how you’d like to deploy your project according to the *type of project* you’re building.

## Deploy Astro in 3 ways

Astro and Netlify offer you **three ways** to deploy your site to Netlify depending on the *type of functionality* you require. You can deploy Astro as a static site, a server-rendered site, or an edge-rendered site.

### Astro as a static site

Astro sites are static sites out of the box. This means that all pages are pre-rendered as static HTML at build-time, cached, and served from the Netlify Content Delivery Network (CDN) on each request. Static sites are suited to content-heavy sites that don’t change often, such as marketing sites, landing pages and documentation. To update changes to your static site, you need to re-build it on Netlify either by pushing to Git, by setting up a [build hook](https://docs.netlify.com/configure-builds/build-hooks/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=docs-build-hooks), or by triggering a build manually in your Netlify dashboard. You don’t need any extra configuration to deploy a static Astro site to Netlify. It just works™.

### Astro as a server-rendered site

If your Astro project relies on dynamic data, such as authentication for login state and e-commerce baskets, or if you need to fetch data from an API at request time, you can configure your project to run as a server-side rendered (SSR) site. This means that every time someone makes a request to your site, the page is built at runtime on the server (powered by Netlify serverless functions), before it's sent back to the browser. You can configure caching — but by default, an Astro SSR site is uncached.

At the time of writing this article, SSR in Astro is currently an all-or-nothing opt-in. When you enable SSR in Astro, **every route** in your `pages` directory becomes a server-rendered route — which on Netlify, uses Netlify Functions under the hood.

To enable SSR in Astro on Netlify, install the Netlify adapter to your project’s dependencies.

```bash
npm install @astrojs/netlify
```

Next, add the adapter to your astro.config.mjs file by importing the dependency, adding the Netlify adapter function to the default export, and specifying output: server.

```javascript
// astro.config.mjs
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';

export default defineConfig({
  output: "server",
  adapter: netlify(),
});
```

[Read more about SSR and Astro on the official Astro documentation](https://docs.astro.build/en/guides/server-side-rendering/).

### Astro as an edge-rendered site

> At the time of writing this article, Netlify Edge Functions and Astro on The Edge are experimental features in beta. Functionality and implementation described below may be subject to change!

We partnered with the Astro team during the launch of [Netlify Edge Functions](https://docs.netlify.com/netlify-labs/experimental-features/edge-functions/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=docs-ef) in April 2022 to give you the option of server-side rendering your whole Astro project at **The Edge**. This means that on every request, Netlify will build the page on the Edge server **at the closest location to the user** before it’s sent back to the browser, meaning your Astro site on Netlify is fast for everyone around the world. This is particularly useful for e-commerce sites, where users need a seamless and fast experience to find what they need quickly and convert — so you can make those sales!

To enable the Netlify Edge adapter in your Astro site, change the `netlify/functions` import line in the Astro config file to use `netlify/edge-functions`.

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

- import netlify from '@astrojs/netlify/functions';
+ import netlify from '@astrojs/netlify/edge-functions';

export default defineConfig({
  output: "server",
  adapter: netlify(),
});
```

Head over to GitHub check out a [complete code example for an Astro project deployed to The Edge on Netlify](https://github.com/sarahetter/astro-netlify-edge-starter). Alternatively, deploy the project to Netlify right now using the Deploy to Netlify button below!

[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/sarahetter/astro-netlify-edge-starter)

Now we've covered the three ways you can deploy a new Astro project to Netlify — static site, server-rendered site, or edge-rendered site — you’re ready to start deploying. Let's take a look at the options.

## Deploying an Astro site

If you deployed the [Netlify Astro Quickstart template](https://github.com/netlify-templates/astro-quickstart) or the [Astro Netlify Edge Starter](https://github.com/sarahetter/astro-netlify-edge-starter) using the Deploy to Netlify buttons, you don’t need to do anything else! Your Astro site is cloned to your GitHub account and already deployed to a live URL on Netlify. You may want to update your site name before you share it with your friends. To do this, navigate to the site in Netlify, and click on Site settings.

![Netlify dashboard header showing the Site settings button is highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnmbefdhmgrmao2sc2vs.png)

Click on Change site name, edit the site name, and click Save.

![Netlify site name settings showing my-new-site-name added to the input field](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiyz39fvd8utggjahe6v.png)
 
### Deploy an existing Git repository to Netlify

Once you’ve got your new Astro project pushed up to GitHub, GitLab, Bitbucket or Azure DevOps via Git, head on over to your Netlify dashboard and click Add new site, and select Import an existing project.

![Dropdown button named Add new site, showing a menu with import an existing project highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zmp0bpw8mr0hs6g1lce.png)

Click on the button for your Git provider of choice, and follow the authentication instructions in your browser.

![Import an existing project from a Git repository, showing buttons to connect GitHub, GitLab, Bitbucket and Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/judsa077u766ro8a84h7.png)

At this stage, you may need to give Netlify access to the Git repository you wish to deploy. Follow the instructions for your particular Git provider. Find your project in the repository list, and click to configure the deployment.

![Pick a repository from GitHub. The search field is populated with the word astro, and one matching repository is shown in the list below](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwtxcmpm151y8zryqy6s.png)

Netlify will automatically detect the build settings for your new Astro site, so unless you’ve done something fancy in your project, you will be good to go with the default settings. Click Deploy site.

![Site settings, build command and publish directory for the new site are already pre-populated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14zyuj1g8f9jjzwijrkx.png)

Next, you’ll be redirected to the site overview page where Netlify has already kicked off a production build. Click into the deployment to watch the build progress.

![A new site with the auto generated URL famous-kangaroo-93944b is currently building](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gumu7xcidcf2skcof402.png)

When the build is complete, click on Open published deploy to view your new Astro site on Netlify! You can now update your site name and URL [as described above](#deploying-an-astro-site).

In the example above I deployed the Astro Netlify Edge Starter repository to my Netlify account. This example project added the Netlify Edge adapter to the Astro config file, and **there were no extra steps to take when deploying an Astro edge-rendered site**! Deploying a server-rendered or edge-rendered Astro site is as straightforward as deploying a static Astro site! 🚀 

### Link your Astro project and deploy using the Netlify CLI

If you love working on the command line, you can also create a new site on Netlify and link up your Git repository using the [Netlify CLI](https://cli.netlify.com/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=cli-docs).

Get started by installing the Netlify CLI globally to your machine using npm.

```bash
npm install netlify-cli -g
```

Log in to the Netlify CLI using `netlify login` in your terminal, and authorize via your browser.

![Netlify login has logged me in to my Netlify account on the CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jlyf7sg1iptrq40znfh.png)

Run `netlify init` to start the process of creating and deploying a new site on Netlify. Choose to Create & configure a new site.

![Create and configure a new site is selected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsd90ito3if11agn1tms.png)

Select your team and choose a name for your new site.

![My new site name is my-astro-site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgee7aa1s9aub25cuip3.png)
  
The Netlify CLI automatically detects the build settings (`astro build`), deploy directory (`dist`), and Netlify Functions directory (should you need it) for your Astro site. Press enter to confirm.

![Netlify has created the site, and is confirming the build command is astro build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhfn629cozz3g31mb72m.png)

The CLI will then offer to automatically generate a `netlify.toml` file with those build and deploy settings. The .toml file is not necessary for this deployment, but you can press Y and enter to confirm if you want to create the file.

![All configuration has been auto-detected and confirmed. I have chosen to create a Netlify toml file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8vxtqbg29qplqj397ja.png)
 
Next, the CLI will add a deploy key to the repository, which means your site will be automatically rebuilt on Netlify every time you push a change to Git. And it’s done! 

![The deploy key has been added to the repository and CI/CD has been configured to automatically deploy from GitHub branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8zh1eijhd0tll9zsokm.png)

Open your Netlify dashboard, and you’ll see a build in progress. Give it a minute, and wait for `Site is live ✨` in the build logs. You successfully linked up your Astro site Git repository and deployed a build to Netlify using the Netlify CLI!

## Further reading

Do you want to learn more about Astro on Netlify? Here’s a list of recommended reading.

- [Astro and Netlify documentation](https://docs.netlify.com/integrations/frameworks/astro/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=docs-frameworks-astro)
- [Astro on Netlify Edge Functions](https://astro.build/blog/netlify-edge-functions/) on the Astro blog
- [We're all living on it. But what exactly is The Edge?](https://whitep4nth3r.com/blog/what-is-the-edge-serverless-functions/)

You can also catch up on a live stream where I was joined by Astro’s very own [Ben Holmes](https://www.netlify.com/blog/authors/ben-holmes/?utm_campaign=devex-san&utm_source=whitep4nth3r-blog&utm_content=view-ben-holmes). In this stream we used the Astro CLI to create a new Astro “Just the basics” project, deployed it to Netlify, integrated React and Svelte into the project, and discussed server-side rendering and edge-rendering an Astro site.

{% embed https://www.youtube.com/embed/A3HDN_dPq7k %}

And finally — have you deployed an Astro project on Netlify? Come show it off in the [Jamstack Discord](https://discord.gg/jamstack)!