Learning objectives
After reading this article you will be able to:
- Build and publish the static output of an Astro site
- Turn on on-demand rendering with the Node adapter in standalone mode
- Avoid the host, port, base path and environment variable mistakes
On this page
Astro is built for content sites: blogs, documentation, product pages. By default it leaves nothing for the server to do. The build turns each page into an HTML file, and hosting comes down to publishing a folder.
That changes when a page has to be put together at request time, with user data or data from a database. Then you add an adapter, and the site gets a Node.js process in production. This guide covers both paths and the details that usually break a deploy.
Static or on demand: what does your site need?#
| Configuration | How pages are generated | What ships to production | Needs an adapter? |
|---|---|---|---|
output: 'static' (default) |
All prerendered at build time | Contents of dist/ |
No |
'static' with prerender = false on some pages |
Static, except the marked ones | dist/ with client/ and server/ |
Yes |
output: 'server' |
All on demand, except prerender = true |
dist/ with client/ and server/ |
Yes |
When in doubt, start static. A static site has no process to crash and no port to configure, and it responds fast because the HTML is ready. Contact forms and search can call a separate API. The differences between the models are in SSR vs SSG vs static, and the comparison between Astro and other generators is in the best static site generator.
How do you build and publish the static site?#
Create the project with the official wizard and run the build:
npm create astro@latest my-site
cd my-site
npm run build
npm run previewNode.js has to meet the minimum version in the Astro documentation; use the active LTS. npm run build writes the site to dist/, and npm run preview serves that folder locally so you can check the result before publishing.
Before the first deploy, set two options in astro.config.mjs:
// astro.config.mjs
import { defineConfig } from "astro/config";
export default defineConfig({
site: "https://www.example.com",
// base: "/blog", // only if the site lives under a subpath
});site is the final address, which Astro uses to generate the sitemap and canonical URLs. base only matters if the site is served under a subpath, such as example.com/blog/.
What you publish is the contents of dist/, with index.html at the root. Source code, node_modules and .env files stay out. For the format in general, see what is a static site.
How should the server answer Astro's URLs?#
With the default build format (build.format: 'directory'), each page becomes a folder with an index.html. The page src/pages/about.astro becomes dist/about/index.html, served at /about/. With build.format: 'file', it becomes dist/about.html. How /about without the trailing slash is handled varies from server to server (a redirect, the file served directly or a 404), so open both forms after deploying and keep internal links to a single pattern.
An Astro site is not an SPA: each page has its own HTML. So do not set up the "every unknown route returns index.html" fallback that React and Vue apps use. It would make any missing URL answer 200 with the home page. Instead, create src/pages/404.astro. The build writes a 404.html, which most hosts find and use.
Files in dist/_astro/ carry a hash in their names, so they can be cached for a long time. HTML should have a short cache so updates show up quickly; see the Cache-Control header.
How do you turn on SSR with the Node adapter?#
Install the adapter. This command installs the package and edits astro.config.mjs for you:
npx astro add nodeThe resulting configuration, in standalone mode, which builds a server ready to run:
// astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
export default defineConfig({
site: "https://www.example.com",
adapter: node({ mode: "standalone" }),
});The other mode, middleware, builds a handler you mount inside your own Express or Fastify server. With the adapter installed, mark the pages that should render on every request:
---
// src/pages/now.astro
export const prerender = false;
const time = new Date().toLocaleTimeString("en-US");
---
<p>Page generated at {time}</p>The build now writes dist/client/ (public files and prerendered pages) and dist/server/ (the server). To start it:
npm run build
HOST=0.0.0.0 PORT=8080 node ./dist/server/entry.mjsIn standalone mode, the server itself delivers the files in dist/client/. It reads HOST and PORT from the environment. Without HOST, it falls back to server.host in astro.config, whose default is false, which the adapter's code turns into listening on localhost only. In production this is the most common mistake: the process starts, but nobody outside can connect. Pass HOST=0.0.0.0 in the command or set server: { host: true } in the config. The general diagnosis is in EADDRINUSE, PORT and 0.0.0.0.
dist/ alone is not enough: the server still imports packages from node_modules. The documentation's Docker recipe installs the production dependencies (npm install --omit=dev) next to dist/ before starting it.
How do you use environment variables in Astro?#
Astro reads variables through import.meta.env, and only those starting with PUBLIC_ reach code that runs in the browser. These values are replaced at build time: they are baked into the files and only change with a new build. On a static site every variable works this way, because there is no server in production.
On pages rendered on demand, the Node adapter also lets you read the environment at runtime through process.env:
---
export const prerender = false;
const apiUrl = import.meta.env.PUBLIC_API_URL; // baked in at build time
const token = process.env.API_TOKEN; // read on every request, server only
---To validate and type your variables, Astro has the astro:env module, where each variable declares its context (client or server) and access (public or secret). Secrets never get the PUBLIC_ prefix. The difference between the two moments is covered in build-time vs runtime variables.
What are the most common errors?#
The build fails with this message when a page has prerender = false, or output is 'server', with no adapter:
NoAdapterInstalled: Cannot use server-rendered pages without an adapter. Please install and configure the appropriate server adapter for your final deployment.Install your host's adapter (npx astro add node for a Node.js server) or turn the page back into a prerendered one. Other frequent problems:
- The server starts but doesn't answer at the public address: it is listening on
localhost. UseHOST=0.0.0.0. - Broken CSS and links: the site lives under a subpath without
base, orbasewas left over after moving to your own domain. - Sitemap and canonical URLs with the wrong domain:
sitewas never updated. import.meta.env.API_URLisundefinedin the browser: the variable lacks thePUBLIC_prefix.- The host's generic 404 page:
src/pages/404.astrois missing. - Process killed during the build: usually lack of memory, common on sites with many optimized images. Build in CI, as in automatic deploys with GitHub Actions.
How to do this on Square Cloud#
A static site runs on Square Cloud's static sites runtime: a web server (nginx) stays up delivering the files, without running any of your code on the server. There is no build step. Run npm run build on your machine or in CI, such as GitHub Actions, and upload the contents of dist/ by ZIP upload in the dashboard, with the CLI, from GitHub or with the VS Code extension. An update shows up right away, with no rebuild or restart.
Don't create a vite.html. That file is the runtime's SPA fallback, which it serves for every path that is not a file. Without it, unknown paths get the 404.html, the same one your src/pages/404.astro generates.
With the Node adapter, the project goes on the Node.js runtime. Build in CI and upload dist/ together with package.json, or put the build in the start command (START). On start, the platform runs npm install when there is no node_modules, with NODE_ENV=production, so devDependencies are not installed. With the build in START, everything the build uses has to be in dependencies (the starter's astro and the adapter added by astro add already are), and the build runs again on every start. The server must listen on 0.0.0.0 on the port in the PORT variable, which the adapter already reads. Two example start commands, to adjust: HOST=0.0.0.0 node ./dist/server/entry.mjs, with dist/ coming from CI, or npm run build && HOST=0.0.0.0 node ./dist/server/entry.mjs, with the build in START.
The squarecloud.app file is optional: the dashboard generates it at deploy time, or you write it at the root of the project. A site uses at least 512 MB of the plan's RAM pool and gets a *.squareweb.app subdomain with HTTPS; custom domains are available from Standard. Every plan includes cookieless web analytics, collected on the server with no script or tag, and Hobby starts at R$12.49 per month. The details are on the static sites runtime page.
Website hosting
Static and SSR sites with automatic TLS and a global CDN
FAQs
Do I need Node.js on the server to host an Astro site?
No, if every page is prerendered, which is the default. The build writes HTML, CSS and JavaScript to dist/, and a static server delivers those files. You only need Node.js in production for pages rendered on demand.
What is the difference between output 'static' and 'server' in Astro?
With 'static', the default, every page is prerendered at build time and you mark exceptions with export const prerender = false. With 'server', every page renders on each request and you mark the static ones with export const prerender = true. Both setups with on-demand pages need an adapter.
Why doesn't my Astro site with the Node adapter respond from outside the server?
Because the standalone server listens on localhost when HOST is not set and server.host in astro.config is left at its default. Start it with HOST=0.0.0.0 or set server.host to true.
Do I need an index.html fallback for an Astro site?
No. Astro writes an HTML file for every page, so the server only has to deliver the files. An index.html fallback would hide pages that don't exist; create src/pages/404.astro to get a 404.html.
More in Websites
See all 25 articles- What is a static site (and how to host one)A static site is a set of ready-made HTML, CSS and JavaScript files served the same to every visitor. Learn how it works, when to use one and how to host it.
- How much does it cost to host a website?Hosting a website costs anywhere from nothing to a few dozen dollars a month. See what goes into the bill: hosting, domain, traffic and extras, with real limits.
- How much does static site hosting cost?Hosting a static site costs between zero and a few dozen dollars a month in most cases. See how traffic sets the price, with step-by-step math.
- How much does it cost to host a Next.js app?Hosting a Next.js app costs from zero with a static export to tens of dollars a month with a server. See the billing models, traffic math and hidden costs.