🔥 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)
Astro Fundamentals••By Komal Nakrani

The Astro Build Process: What astro build Actually Does

The Astro build process step by step: config and integration hooks, content sync, Vite environments, static generation, image optimization and the final dist output.

#Astro#Astro Build Process#Vite#Build#Static Site Generation

The Astro build process runs in a fixed order: Astro loads astro.config.mjs and runs integration setup hooks, syncs content collections and generated types, bundles your code with Vite into separate prerender, server and client builds, renders every static route to HTML, optimizes the images those pages use, copies public/, and finally runs astro:build:done hooks before printing how many pages it built. The result lands in dist/.

Knowing that order explains most build errors and most build-time questions: why a missing frontmatter field fails before any page renders, why the sitemap appears last, and where the time goes on a large site.

Key Takeaways

  • Configuration and integrations load first. Errors in astro.config.mjs or an integration’s setup stop the build before anything else runs.
  • Content collections are synced and validated before bundling, so schema errors appear early with the file name.
  • Astro 7 uses Vite 8’s environments: a prerender build for static pages, an ssr build for on-demand routes when you have them, and a client build for browser JavaScript and CSS.
  • Static generation calls getStaticPaths() and renders each page to HTML. This is usually the slowest phase on large sites.
  • Image optimization runs after pages render, because Astro only knows which image sizes are needed once the HTML exists.

The terminal output, decoded

A typical static build prints something like this (trimmed):

$ npm run build

> astro build

[build] output: "static"
[build] mode: "static"
[build] directory: /my-site/dist/
[build] Collecting build info...
[build] ✓ Completed in 210ms.
[build] Building static entrypoints...
[build] ✓ Completed in 3.1s.

 generating static routes
  ├─ /index.html
  ├─ /about/index.html
  ├─ /blog/first-post/index.html
  ...
✓ Completed in 1.2s.

 generating optimized images
  ├─ /_astro/hero.Bx2k9a.webp
  ...
✓ Completed in 2.4s.

[build] 48 page(s) built in 7.3s
[build] Complete!

Each block maps to a phase below. With an adapter and on-demand routes, the header says “prerendering static routes” instead, and the summary reads “Server built in” rather than a page count.

Phase 1: configuration and integrations

Astro reads astro.config.mjs, validates it against its config schema, and applies defaults. An unknown option or a wrong type fails here with a message naming the option.

Then each integration’s astro:config:setup hook runs. This is where integrations add their own Vite plugins, inject routes (the sitemap integration doesn’t, but many do), inject scripts (Partytown adds its loader here), or register renderers for React, Vue or Svelte components. After every integration has run, astro:config:done fires with the final config.

If you see an error before any “[build]” line, it’s almost always from this phase: a syntax error in the config, an integration version incompatible with your Astro version, or an integration throwing during setup.

Phase 2: sync content and types

Next Astro runs the same work as astro sync:

  • Each content collection’s loader runs. The glob() loader reads your Markdown, MDX, JSON or YAML files; custom loaders might fetch from a CMS.
  • Every entry is validated against the collection’s schema. A post with a missing title or an invalid date fails the build here, with the file path and the field.
  • The results go into Astro’s content data store, and TypeScript types for your collections and astro:env variables are generated in the .astro/ folder.

Because this happens before bundling, a content error appears in seconds, not after minutes of rendering. The Content Collections guide covers schemas, and the frontmatter schema error tip explains the most common failure.

Phase 3: bundling with Vite

The astro:build:start hook fires, Astro logs the output mode and adapter, and then hands your code to Vite. In Astro 7 this is Vite 8, which bundles with Rolldown.

Astro uses Vite’s environments API to run separate builds for different targets:

Environment What it builds When
prerender Server-side code that renders your static pages during the build Always, when any page is prerendered
ssr The server entrypoint your adapter runs for on-demand routes Only with an adapter and on-demand routes
client JavaScript and CSS that ship to the browser Always

During bundling, every .astro file is compiled to a JavaScript module by the Astro compiler. Component <style> blocks are scoped and extracted into CSS files; <script> tags are bundled as modules; islands with client:* directives become separate client chunks, loaded only on pages that use them. Before Vite runs, the astro:build:setup hook lets integrations adjust the Vite config for each environment.

The client output gets content-hashed filenames in dist/_astro/ (the folder is configurable with build.assets), such as index.B3kq2xZ1.css. The hash changes when content changes, which is why these files can be cached for a year. The How Astro uses Vite explainer covers this layer in detail.

Phase 4: generating static routes

Now Astro runs the prerender bundle to produce HTML. For each route in src/pages/:

  1. If the route is dynamic ([slug].astro), Astro calls its getStaticPaths() to get the list of params and props.
  2. For each path, it renders the page: runs the component frontmatter, renders child components, resolves await calls, and produces an HTML string.
  3. It writes the file to dist/. With the default build.format: 'directory', /about becomes dist/about/index.html.

Code in component frontmatter runs here, at build time, once per page. A fetch() in frontmatter hits the API during the build, not when visitors arrive. A slow API call inside a component used on 500 pages runs 500 times unless you cache the result in a module-level variable.

Pages render with a concurrency of 1 by default. build.concurrency raises it, which can help when pages spend their time waiting on I/O. Astro 7.4 also added an experimental parallel prerendering option for large sites, and astro build --timings to see exactly which phase and which routes are slow.

On repeated builds, the experimental incrementalBuild flag (added in Astro 7.2) can skip pages whose cacheKey from getStaticPaths() and module dependencies haven’t changed since the last build.

Phase 5: generating optimized images

Only after every page has rendered does Astro know exactly which images, at which widths and formats, the HTML references. Each <Image>, <Picture> and getImage() call during rendering registers a transform. Astro then processes the list with Sharp (the default image service), writing files like hero.Bx2k9a_Z1abc.webp into dist/_astro/.

This phase is often the second slowest, and it grows with the number of image sizes, not just the number of images. Four widths across two formats is eight files per image. Astro caches transformed images in node_modules/.astro/ between builds, so persisting that folder in CI saves real time; the CI build cache tip shows how.

Remote images are downloaded and processed the same way, as long as their domains are allowed in image.domains or image.remotePatterns.

Phase 6: public files and server output

Files in public/ are copied to dist/ unchanged: robots.txt, favicon.svg, _headers, fonts you reference by URL. Astro doesn’t process, hash or optimize them, which is why images placed there miss out on optimization.

With an adapter and on-demand routes, the output is split. Browser assets and prerendered HTML go into dist/client/, and the server entrypoint goes into dist/server/ (adapters often reshape this into their host’s format, such as .vercel/output/). Astro moves assets between the two builds so each file ends up where the host expects it.

Phase 7: build done hooks

Last, astro:build:done runs for each integration, with the list of built pages and the output directory. This is where post-processing happens:

  • @astrojs/sitemap writes sitemap-index.xml and sitemap-0.xml from the final page list.
  • Partytown copies its library files into dist/~partytown/.
  • Compression or HTML minification integrations rewrite files in place.
  • Search indexers like Pagefind (usually run as a separate script after astro build) read the finished HTML.

Then Astro prints the summary line and exits.

Where build errors come from

When it fails Likely phase Typical cause
Before any [build] output Config Invalid config option, incompatible integration
Right after “Collecting build info” Content sync Frontmatter doesn’t match schema, loader error
During “Building entrypoints” Vite bundling Import of a missing file, syntax error, server-only module in client code
During “generating static routes” Rendering getStaticPaths() missing or returning bad params, a runtime error in a component
During “generating optimized images” Images Missing Sharp, unsupported format, remote domain not allowed
After pages and images Integrations A build:done hook failing

Reading the last phase header before the error narrows the search to one part of your code.

What’s in dist

A static build leaves a folder you can upload anywhere:

dist/
├── _astro/              # hashed CSS, JS and optimized images
├── about/index.html
├── blog/first-post/index.html
├── index.html
├── 404.html
├── robots.txt           # copied from public/
└── sitemap-index.xml    # written by @astrojs/sitemap

No server, no runtime. That’s why a static Astro site can be hosted on Cloudflare Pages, Netlify, Vercel, GitHub Pages or a plain web server. Komal themes are built this way: npm run build writes plain files to dist/, and the folder structure docs show how a theme’s src/ maps onto that output. If you want to see the process on a real project, you can browse Astro themes, clone one and run the build yourself. The official CLI reference for astro build lists its flags.

FAQ

What does astro build do?

It loads your config and integrations, validates content collections, bundles your code with Vite, renders every static page to HTML, optimizes images, copies public/, runs integration hooks such as sitemap generation, and writes the finished site to dist/.

Where does Astro put the build output?

In dist/ by default, configurable with outDir. With an adapter and on-demand routes, browser assets go to dist/client/ and server code to dist/server/, unless the adapter writes its own format.

Why is my Astro build slow?

The usual causes are many pages each making the same slow data request, many images at many widths, or heavy remark or rehype processing. Cache data requests at module level, reduce image widths, persist node_modules/.astro in CI, and use astro build --timings to find the slow phase.

Does astro build run getStaticPaths?

Yes. During static generation, Astro calls getStaticPaths() once for each dynamic route, then renders a page for every path it returns.

What is the difference between astro build and astro preview?

astro build creates the production site in dist/. astro preview starts a local server that serves that built output so you can check it before deploying.