Discover how creating pages with Astro saves you from endless copying and broken links!
Creating pages in Astro - Why You Should Know This
Imagine building a website where you have to create a new HTML file for every page manually, linking each one by hand and copying the same header and footer everywhere.
This manual process is slow, repetitive, and easy to make mistakes like broken links or inconsistent layouts. Updating the site means changing every page one by one.
Astro's page creation lets you build pages as components that automatically handle routing and layout, so you write less code and keep your site consistent and easy to update.
<!DOCTYPE html> <html> <head><title>Home</title></head> <body> <header>...</header> <p>Welcome!</p> <footer>...</footer> </body> </html>
---
---
<html>
<head><title>Home</title></head>
<body>
<Header />
<p>Welcome!</p>
<Footer />
</body>
</html>It enables you to build scalable websites quickly with reusable page components and automatic routing.
Think of a blog where each new post is a new page created easily without copying full HTML, saving hours of work and avoiding errors.
Manual page creation is repetitive and error-prone.
Astro automates page setup with components and routing.
This makes building and updating sites faster and safer.
Practice
.astro file inside the src/pages folder in an Astro project?Solution
Step 1: Understand Astro's file-based routing
Astro automatically creates routes from files insrc/pages. Each.astrofile becomes a page.Step 2: Connect file name to URL
The file name directly maps to the URL path, so creating a file adds a new page accessible by that URL.Final Answer:
A new webpage is automatically created with a URL matching the file name. -> Option CQuick Check:
File insrc/pages= new page URL [OK]
- Thinking you must import page files manually
- Confusing components with pages
- Believing a routing config is needed
Solution
Step 1: Identify homepage naming convention
In Astro, the homepage URL is/, which maps toindex.astroinsrc/pages.Step 2: Check folder and file placement
The file must be directly insidesrc/pagesand namedindex.astroto serve as homepage.Final Answer:
Create a file namedindex.astroinsidesrc/pages. -> Option DQuick Check:
Homepage =src/pages/index.astro[OK]
index.astro in pages folder [OK]- Naming homepage file anything other than index.astro
- Placing homepage file in components folder
- Using nested folders incorrectly for homepage
src/pages/about.astro with content:<!-- src/pages/about.astro --> <h1>About Us</h1> <p>Welcome to our site.</p>
What URL will display this page when running the Astro dev server?
Solution
Step 1: Map file path to URL
The fileabout.astroinsidesrc/pagesmaps to the URL path/about.Step 2: Understand URL structure
Astro removes the file extension and folder prefixsrc/pagesfrom the URL, so the URL is/about.Final Answer:
http://localhost:3000/about -> Option AQuick Check:
File name = URL path [OK]
- Including .astro extension in URL
- Adding 'pages' folder name to URL
- Guessing URL from page content
src/pages/contact.astro but visiting /contact shows a 404 error. What is the most likely cause?Solution
Step 1: Verify file placement
The most likely cause is that the file is actually insidesrc/componentsinstead ofsrc/pages. Components folder files do not create routes.Step 2: Syntax errors cause error pages, not 404
Syntax errors show an error screen when visiting the route, not a 404 Page Not Found.Step 3: Dev server auto-reloads
Astro detects new pages automatically, restart rarely needed.Final Answer:
The file is placed insidesrc/componentsinstead ofsrc/pages. -> Option BQuick Check:
404 on new page = check pages vs components folder [OK]
- Assuming routing config is needed
- Not checking file syntax
- Misplacing file in components folder
/blog/post1. Which file structure and naming is correct in Astro?Solution
Step 1: Understand nested routing in Astro
Folders insidesrc/pagescreate nested URL paths automatically.Step 2: Map file to nested URL
Placingpost1.astroinsidesrc/pages/blogcreates URL/blog/post1.Step 3: Check alternatives
Createsrc/pages/blog-post1.astrofile creates flat URL/blog-post1. Createsrc/pages/blog/post1/index.htmlfile creates no route (.html not supported as pages). Createsrc/pages/blog/post1.astroand add a routing config is incorrect because Astro does not require routing config.Final Answer:
Createsrc/pages/blog/post1.astrofile. -> Option AQuick Check:
Nested folder + file = nested URL [OK]
- Using hyphens instead of folders for nesting
- Adding unnecessary routing config
- Confusing index.astro usage for nested pages
Ready to prove it?
Pass the test and this pattern moves unknown
