404 and error pages help visitors know when something went wrong or a page is missing. They make your website friendlier and easier to use.
404 and error pages in Astro
--- export const prerender = true; --- <html lang="en"> <head> <title>Page Not Found</title> </head> <body> <h1>404 - Page Not Found</h1> <p>Sorry, we couldn't find the page you were looking for.</p> <a href="/">Go back home</a> </body> </html>
Astro uses special files like 404.astro to show custom 404 pages.
Setting export const prerender = true; helps Astro generate the page at build time.
--- export const prerender = true; --- <html lang="en"> <head><title>404 Not Found</title></head> <body> <h1>Oops! Page not found.</h1> <p>Try checking the URL or go back home.</p> <a href="/">Home</a> </body> </html>
--- export const prerender = true; --- <html lang="en"> <head><title>Error</title></head> <body> <h1>Something went wrong.</h1> <p>Please try again later or contact support.</p> <a href="/">Home</a> </body> </html>
This is a complete 404 page in Astro. It shows a clear message, uses simple styling, and includes a link with an accessible label to help users return home.
--- export const prerender = true; --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>404 - Not Found</title> <style> body { font-family: system-ui, sans-serif; text-align: center; padding: 4rem; background-color: #f9f9f9; color: #333; } a { color: #007acc; text-decoration: none; font-weight: bold; } a:hover { text-decoration: underline; } </style> </head> <body> <h1>404</h1> <p>Sorry, the page you are looking for does not exist.</p> <a href="/" aria-label="Go back to homepage">Go back home</a> </body> </html>
Always create a 404.astro file in your src/pages folder to handle missing pages.
Use clear, friendly language to help visitors understand what happened.
Include links to help users navigate back to working parts of your site.
404 and error pages improve user experience by explaining problems clearly.
Astro uses special files like 404.astro to show these pages automatically.
Keep messages simple, friendly, and provide navigation options.
Practice
404.astro page in an Astro project?Solution
Step 1: Understand the role of 404 pages
A 404 page appears when a user tries to visit a page that the website cannot find.Step 2: Identify the purpose in Astro
In Astro,404.astrois used to show a friendly message for missing pages.Final Answer:
To show a friendly message when a user visits a page that does not exist -> Option DQuick Check:
404 page = friendly missing page message [OK]
- Thinking 404 pages speed up the homepage
- Confusing 404 pages with login pages
- Assuming 404 pages show site maps
Solution
Step 1: Recall Astro's naming convention for 404 pages
Astro uses the exact file name404.astroto detect and serve 404 error pages automatically.Step 2: Compare options with the required file name
Only404.astromatches the required name; others won't be recognized automatically.Final Answer:
404.astro -> Option CQuick Check:
404 page file = 404.astro [OK]
- Using names like error.astro or notfound.astro
- Adding prefixes or suffixes to 404 file name
- Assuming any file with '404' works
404.astro code snippet, what will the user see when visiting a missing page?<!-- 404.astro --> <h1>Oops! Page not found.</h1> <p>Try going back to the <a href='/'>homepage</a>.</p>
Solution
Step 1: Read the 404.astro content
The code shows a heading with 'Oops! Page not found.' and a paragraph with a homepage link.Step 2: Understand what Astro renders on missing pages
Astro will display this content when a user visits a missing page.Final Answer:
A message saying 'Oops! Page not found.' with a link to homepage -> Option BQuick Check:
404 page shows friendly message + homepage link [OK]
- Expecting a blank page instead of message
- Confusing 404 with server error 500
- Thinking homepage content shows on 404
404.astro page but it does not show when visiting a missing page. What is the most likely mistake?Solution
Step 1: Check file location requirements in Astro
Astro expects404.astroto be in the root of thesrc/pagesfolder, not inside components.Step 2: Understand why wrong location breaks 404 page
If placed insrc/components, Astro won't serve it automatically as a 404 page.Final Answer:
The404.astrofile is inside thesrc/componentsfolder -> Option AQuick Check:
404 page must be in pages folder [OK]
- Naming file 404.html instead of 404.astro
- Putting 404.astro in components folder
- Thinking missing homepage link breaks 404 page
- Assuming package.json affects 404 page
Solution
Step 1: Understand Astro's error page handling
Astro supports special files like404.astroand500.astroto handle different error codes automatically.Step 2: Choose the best way to show different messages
Creating separate files for 404 and 500 errors is clean and follows Astro conventions.Final Answer:
Create separate404.astroand500.astrofiles insrc/pages-> Option AQuick Check:
Separate files for each error code [OK]
- Trying to detect errors with JavaScript in 404.astro
- Renaming files manually instead of separate files
- Ignoring server-side error handling
Ready to prove it?
Pass the test and this pattern moves unknown
