All guides

Guide

How to build your site with Astro

Astro is a tool for building websites that are mostly content: marketing sites, blogs and shops. Its makers describe it as a framework for content-driven websites. It sends very little code to the visitor's browser, which is a large part of why Astro sites load quickly. This guide explains enough to run one, change it and put it online.

A man working on a laptop at a wooden table beside a bookshelf and a window.

What you need on your computer

  • Node.js. This is the program that runs Astro on your computer. Astro needs version 22 or newer, and it does not support the odd-numbered versions such as 23. The README in your template gives the exact version.
  • Your project folder. If you bought one of our Astro templates, you get access to it on GitHub and download a copy.
  • A terminal, or an AI agent that can use one. The commands below are typed into a terminal. If you use Claude Code, you can ask it to run them for you.

The four commands

CommandWhat it does
npm installDownloads everything the site needs. You run it once, and again if the project's requirements change.
npm run devStarts a preview of the site on your own computer. It prints a web address to open, and the page updates as you make changes.
npm run buildProduces the finished site in a folder called dist. This is what goes online.
npm run previewShows you the finished site from dist on your computer, so you can check it before it goes online.
A terminal after typing npm run dev, showing the address http://localhost:4321/ to open in a browser.

Where things live

  • src/pages holds the pages. Each file becomes a page on your site, and the file's name becomes its address. A file called about.astro becomes /about.
  • src/components holds the pieces that pages are built from, such as the navigation, the footer and cards.
  • src/layouts holds the frame that wraps every page.
  • src/styles holds the design settings. In our templates the colours, type sizes and spacing are at the top of global.css.
  • public holds files that are used as they are, such as images, fonts and the browser tab icon.
  • astro.config.mjs holds the project's settings.
The main folders of an Astro project and what each one holds: pages, components, layouts, styles and public.

Where the content comes from

An Astro site can keep its content in files inside the project, or fetch it from a content system such as Sanity. Our Astro templates do both. They work straight away using sample content stored in the project, and you can connect Sanity later so that content is edited in a dashboard and not in code. See the Sanity guide.

One thing to understand early: a standard Astro site is built ahead of time. When content changes, the site has to be built again before visitors see the change. Hosting services can do that automatically.

How to put it online

The usual way is through a hosting service such as Vercel or Netlify.

  1. Put your project on GitHub.
  2. Sign in to the host and import the project from GitHub.
  3. Confirm the settings. Vercel recognises Astro on its own. On Netlify the build command is npm run build and the publish folder is dist.
  4. Add your settings, described next.
  5. Deploy.

After that, each time you send a change to the main branch on GitHub, the host rebuilds the site and puts it live. Changes on other branches get their own preview address, so you can look before it goes live.

Three steps: you send a change to GitHub, the host rebuilds the site, and the change is live.

Settings that live outside the code

Some values should not be written into the code, such as your site address or the ID of your Sanity project. These are called environment variables. On your computer they go in a file named .env. On the host you type them into its settings page.

An example .env file with a site address, a Sanity project ID, a dataset name and a hidden token.

Two things catch people out:

More guides

All guides

See the finished site before you buy it

Open any live preview, click through every page, then choose the format you want to build in.