Routes & ship · Lesson 14 of 22 · 10 min
Routes, a 404 page, and the build
See how every file maps to a URL, add a custom 404, and check your production build.
How your files became URLs
Astro uses file-based routing. The path of a file inside src/pages/ is its URL. Here’s your whole site:
| File | URL |
|---|---|
pages/index.astro |
/ |
pages/about.md |
/about |
pages/blog/index.astro |
/blog |
pages/blog/[id].astro |
/blog/first-supper, /blog/fall-menu, … |
pages/404.astro |
any URL that doesn’t exist |
The rules:
indextakes the URL of its folder.- Folders add a segment to the URL.
[brackets]make a segment dynamic, filled in bygetStaticPaths.[...rest](with three dots) matches any number of segments, like/docs/a/b/c. You don’t need it here, but now you know it exists.- Files outside
src/pages/never become URLs. That’s why components and layouts are safe in their own folders.
1. Add a custom 404 page
src/pages/404.astro
---
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="Page not found">
<div class="container">
<h1>That page isn't on the menu.</h1>
<p><a href="/">Head back home</a></p>
</div>
</BaseLayout>
<style>
h1 {
margin-top: 3rem;
}
</style>
Visit any made-up URL, like /nope. Vercel and Netlify both serve 404.html automatically for missing pages.
2. Build for production
Stop the dev server and run:
npm run build
You’ll see Astro list every page it generated:
generating static routes
├─ /404.html
├─ /about/index.html
├─ /blog/fall-menu/index.html
├─ /blog/first-supper/index.html
├─ /blog/meal-prep-tips/index.html
├─ /blog/index.html
├─ /index.html
7 page(s) built
Everything lands in dist/. That folder is your whole website: plain HTML, CSS, and nothing else. Open dist/index.html in your editor and look. No framework code.
3. Preview the real build
npm run preview
This serves dist/ locally. Click through every page. This is exactly what visitors will get.