The not-found.tsx file lets you show a friendly message when a page is missing. It helps visitors understand they reached a wrong or deleted page.
Not-found.tsx customization in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NextJS
export default function NotFound() { return ( <main> <h1>Page Not Found</h1> <p>Sorry, we couldn't find what you were looking for.</p> </main> ) }
This is a React functional component.
Next.js automatically uses this file when a page is not found.
Examples
NextJS
export default function NotFound() { return <h1>Oops! This page does not exist.</h1> }
NextJS
export default function NotFound() { return ( <main> <h1>404 - Page Not Found</h1> <p>Try going back to the <a href="/">homepage</a>.</p> </main> ) }
NextJS
"use client" export default function NotFound() { return ( <main aria-label="Not Found Page"> <h1>Sorry, we can't find that page.</h1> <button onClick={() => window.history.back()}>Go Back</button> </main> ) }
Sample Program
This example shows a styled 404 page with a clear message and a link back to the homepage. It uses semantic HTML and accessibility features like aria-label.
NextJS
import Link from 'next/link' export default function NotFound() { return ( <main role="main" style={{ padding: '2rem', textAlign: 'center' }}> <h1 style={{ fontSize: '2.5rem', color: '#cc0000' }}>404 - Page Not Found</h1> <p style={{ margin: '1rem 0' }}> Sorry, the page you are looking for does not exist. </p> <Link href="/" style={{ color: '#0070f3', textDecoration: 'underline' }} aria-label="Go to homepage">Go back to homepage</Link> </main> ) }
Important Notes
Always use semantic HTML tags like <main> and headings for accessibility.
Provide navigation options so users don't get stuck on the error page.
Keep the design consistent with your website style for a smooth experience.
Summary
The not-found.tsx file customizes what users see when a page is missing.
Use clear messages and helpful links or buttons to guide users.
Make sure your error page is accessible and visually consistent.
Practice
1. What is the main purpose of the
not-found.tsx file in a Next.js app?easy
Solution
Step 1: Understand the role of
This file is specifically for customizing the page shown when a user tries to access a page that does not exist.not-found.tsxStep 2: Compare with other options
Other options like layout, authentication, or API routes are handled by different files or folders in Next.js.Final Answer:
To customize the page shown when a user visits a missing URL -> Option CQuick Check:
Missing page display = Customnot-found.tsx[OK]
Hint: Remember: not-found.tsx shows missing page content [OK]
Common Mistakes:
- Confusing not-found.tsx with layout or API files
- Thinking it handles authentication
- Assuming it manages site navigation
2. Which of the following is the correct way to export a default React component in
not-found.tsx?easy
Solution
Step 1: Recall correct default export syntax
In React with Next.js, the correct way is to writeexport default function ComponentName() { ... }.Step 2: Check each option
export default function NotFound() { returnPage Not Found
; } matches the correct syntax. function NotFound() { returnPage Not Found
; } export NotFound; has incorrect export syntax. export function NotFound() { returnPage Not Found
; } exports a named function, not default. default export function NotFound() { returnPage Not Found
; } uses invalid syntax.Final Answer:
export default function NotFound() { return <h1>Page Not Found</h1>; } -> Option DQuick Check:
Default export =export default function[OK]
Hint: Default export uses 'export default function' syntax [OK]
Common Mistakes:
- Using named export instead of default
- Incorrect export keyword placement
- Missing 'default' keyword
3. Given this
not-found.tsx component, what will be displayed when a user visits a missing page?export default function NotFound() {
return (
<main role="main">
<h1>Oops! Page not found.</h1>
<p>Try going back or visit the homepage.</p>
<a href="/">Home</a>
</main>
);
}medium
Solution
Step 1: Read the returned JSX
The component returns a main section with a heading, paragraph, and a link to the homepage.Step 2: Understand what renders on missing page
When a page is missing, this component renders the content exactly as coded, showing the heading, paragraph, and link.Final Answer:
A page with a heading 'Oops! Page not found.', a paragraph, and a link to Home -> Option AQuick Check:
JSX content renders as visible page elements [OK]
Hint: JSX inside return shows on missing page [OK]
Common Mistakes:
- Thinking it redirects automatically
- Assuming no content shows
- Confusing console errors with UI output
4. Identify the error in this
not-found.tsx code snippet:export default function NotFound() {
return (
<div>
<h1>Page Not Found</h1>
<a href="/">Go Home</a>
</div>
)
}medium
Solution
Step 1: Check JSX syntax
The JSX is correctly wrapped and returned; no syntax errors or missing semicolons needed after return.Step 2: Consider accessibility best practices
Using <main> instead of <div> improves accessibility by defining the main content region.Final Answer:
Using <div> instead of <main> for accessibility -> Option AQuick Check:
Use <main> for main content in error pages [OK]
Hint: Use <main> tag for main content, not just <div> [OK]
Common Mistakes:
- Thinking semicolons are required after return
- Ignoring accessibility tags
- Assuming JSX needs extra parentheses
5. You want your
not-found.tsx page to show a custom message only if the user is on a mobile device, otherwise show a default message. Which approach fits Next.js best?hard
Solution
Step 1: Understand client vs server rendering
Device detection usingwindow.navigator.userAgentworks only on client side, so use React hooks likeuseEffect.Step 2: Evaluate other options
Server-side detection is complex and not typical innot-found.tsx. CSS media queries only style but don't change content. Multiple files for one route is invalid.Final Answer:
Use a React hook likeuseEffectwithwindow.navigator.userAgentto detect device and conditionally render -> Option BQuick Check:
Client detection with hooks = best for device-specific UI [OK]
Hint: Detect device client-side with hooks, not server or multiple files [OK]
Common Mistakes:
- Trying to detect device server-side in this file
- Using multiple not-found.tsx files
- Relying only on CSS for content changes
