Bird
Raised Fist0
Astroframework~20 mins

404 and error pages in Astro - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Astro 404 & Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a 404 page is triggered in Astro?
In Astro, when a user navigates to a URL that does not match any route, what is the default behavior if a src/pages/404.astro file exists?
AThe server returns a blank page with a 200 HTTP status code.
BThe user is redirected to the homepage automatically without showing the 404 page.
CAstro throws a runtime error and the page fails to load.
DThe 404.astro page is rendered and shown to the user with a 404 HTTP status code.
Attempts:
2 left
💡 Hint
Think about how static site generators handle unmatched routes with a 404 page.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to set a custom HTTP status code in an Astro error page
You want to create a custom error page in Astro that returns a 500 status code. Which code snippet correctly sets the status code in src/pages/500.astro?
A
---
Astro.response.statusCode = 500;
---
<h1>Server Error</h1>
B
---
export const prerender = false;
Astro.status = 500;
---
<h1>Server Error</h1>
C
---
Astro.response.status = 500;
---
<h1>Server Error</h1>
D
---
Astro.statusCode = 500;
---
<h1>Server Error</h1>
Attempts:
2 left
💡 Hint
Astro exposes the response object to set HTTP status codes.
🔧 Debug
advanced
2:00remaining
Why does the custom 404 page not show and a blank page appears instead?
You created src/pages/404.astro with valid content, but when visiting a non-existent URL, the browser shows a blank page with no errors. What is the most likely cause?
AThe 404.astro file is placed inside a subfolder instead of directly under src/pages.
BThe 404.astro page is not exported as a default component, causing rendering to fail silently.
CThe 404.astro file is missing the frontmatter script block, so Astro cannot process it.
DThe server is caching an old version and needs to be restarted to pick up the 404 page.
Attempts:
2 left
💡 Hint
Check the file location carefully for routing to work.
❓ component_behavior
advanced
2:00remaining
How does Astro handle error boundaries in components for error pages?
In Astro, if a component inside a page throws an error during rendering, what happens if you have an error boundary component wrapping it?
AThe error boundary catches the error and renders its fallback UI without crashing the whole page.
BAstro ignores error boundaries and always shows the default error page.
CThe entire page fails to render and returns a 500 status code without any UI.
DThe error is logged but the component renders empty space silently.
Attempts:
2 left
💡 Hint
Think about how error boundaries work in component-based frameworks.
❓ state_output
expert
3:00remaining
What is the HTTP status code and rendered output when throwing an error inside src/pages/[slug].astro without a custom error page?
Consider an Astro project with a dynamic route [slug].astro. If this page throws an error during server rendering and no custom error page is defined, what will the HTTP status code be and what will the user see?
AHTTP status 404 with a blank page because no 404 page exists.
BHTTP status 500 with Astro's default error page showing the error message.
CHTTP status 200 with partial page content and an error logged in console.
DHTTP status 302 redirecting to the homepage automatically.
Attempts:
2 left
💡 Hint
Think about server errors and default error handling in Astro.

Practice

(1/5)
1. What is the main purpose of a 404.astro page in an Astro project?
easy
A. To speed up the loading of the homepage
B. To display the site map automatically
C. To handle user login and authentication
D. To show a friendly message when a user visits a page that does not exist

Solution

  1. 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.
  2. Step 2: Identify the purpose in Astro

    In Astro, 404.astro is used to show a friendly message for missing pages.
  3. Final Answer:

    To show a friendly message when a user visits a page that does not exist -> Option D
  4. Quick Check:

    404 page = friendly missing page message [OK]
Hint: 404 pages show missing page messages [OK]
Common Mistakes:
  • Thinking 404 pages speed up the homepage
  • Confusing 404 pages with login pages
  • Assuming 404 pages show site maps
2. Which file name should you create in an Astro project to automatically handle 404 errors?
easy
A. error.astro
B. notfound.astro
C. 404.astro
D. page404.astro

Solution

  1. Step 1: Recall Astro's naming convention for 404 pages

    Astro uses the exact file name 404.astro to detect and serve 404 error pages automatically.
  2. Step 2: Compare options with the required file name

    Only 404.astro matches the required name; others won't be recognized automatically.
  3. Final Answer:

    404.astro -> Option C
  4. Quick Check:

    404 page file = 404.astro [OK]
Hint: Use exact file name 404.astro for error pages [OK]
Common Mistakes:
  • Using names like error.astro or notfound.astro
  • Adding prefixes or suffixes to 404 file name
  • Assuming any file with '404' works
3. Given this 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>
medium
A. A blank page with no message
B. A message saying 'Oops! Page not found.' with a link to homepage
C. An error message with code 500
D. The homepage content without any error message

Solution

  1. Step 1: Read the 404.astro content

    The code shows a heading with 'Oops! Page not found.' and a paragraph with a homepage link.
  2. Step 2: Understand what Astro renders on missing pages

    Astro will display this content when a user visits a missing page.
  3. Final Answer:

    A message saying 'Oops! Page not found.' with a link to homepage -> Option B
  4. Quick Check:

    404 page shows friendly message + homepage link [OK]
Hint: 404 page content shows on missing pages [OK]
Common Mistakes:
  • Expecting a blank page instead of message
  • Confusing 404 with server error 500
  • Thinking homepage content shows on 404
4. You created a 404.astro page but it does not show when visiting a missing page. What is the most likely mistake?
medium
A. The 404.astro file is inside the src/components folder
B. The file is named 404.html instead of 404.astro
C. The homepage link is missing inside the 404 page
D. The project does not have a package.json file

Solution

  1. Step 1: Check file location requirements in Astro

    Astro expects 404.astro to be in the root of the src/pages folder, not inside components.
  2. Step 2: Understand why wrong location breaks 404 page

    If placed in src/components, Astro won't serve it automatically as a 404 page.
  3. Final Answer:

    The 404.astro file is inside the src/components folder -> Option A
  4. Quick Check:

    404 page must be in pages folder [OK]
Hint: Place 404.astro in src/pages folder [OK]
Common Mistakes:
  • 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
5. You want to create a custom error page in Astro that shows different messages for 404 and 500 errors. Which approach is best?
hard
A. Create separate 404.astro and 500.astro files in src/pages
B. Use only 404.astro and add JavaScript to detect 500 errors
C. Create a single error.astro file and rename it to 404.astro when needed
D. Handle errors only in client-side JavaScript, no Astro pages needed

Solution

  1. Step 1: Understand Astro's error page handling

    Astro supports special files like 404.astro and 500.astro to handle different error codes automatically.
  2. Step 2: Choose the best way to show different messages

    Creating separate files for 404 and 500 errors is clean and follows Astro conventions.
  3. Final Answer:

    Create separate 404.astro and 500.astro files in src/pages -> Option A
  4. Quick Check:

    Separate files for each error code [OK]
Hint: Use separate 404.astro and 500.astro files [OK]
Common Mistakes:
  • Trying to detect errors with JavaScript in 404.astro
  • Renaming files manually instead of separate files
  • Ignoring server-side error handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard