Bird
Raised Fist0
NextJSframework~10 mins

Not-found page handling in NextJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Not-found page handling
User requests URL
↓
Next.js checks routes
↓
Route found
↓
Render page
↓
Show 404 page
↓
User sees error message
When a user visits a URL, Next.js checks if the route exists. If not, it shows the custom 404 page from not-found.js.
Execution Sample
NextJS
export default function NotFound() {
  return <h1>404 - Page Not Found</h1>;
}
This code defines a simple custom 404 page that Next.js shows when a page is not found.
Execution Table
StepActionRoute Exists?Component RenderedUser Sees
1User requests /aboutYesAbout page componentAbout page content
2User requests /random-pageNoNotFound component404 - Page Not Found
3User requests /contactYesContact page componentContact page content
4User requests /nonexistentNoNotFound component404 - Page Not Found
💡 Execution stops after rendering the appropriate page or the 404 page if route not found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
requestedRouteundefined/about/random-page/contact/nonexistent
routeExistsundefinedtruefalsetruefalse
renderedComponentundefinedAboutNotFoundContactNotFound
Key Moments - 2 Insights
Why does Next.js show the NotFound component when the route does not exist?
Because Next.js checks if the requested route exists (see execution_table rows 2 and 4). If it doesn't, it renders the NotFound component to show a 404 page.
Can you customize the 404 page in Next.js?
Yes, by creating a not-found.js file exporting a React component, Next.js uses it automatically for missing routes (see execution_sample).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what component is rendered when the user requests '/random-page'?
ANotFound component
BAbout page component
CContact page component
DHome page component
💡 Hint
Check the row where requestedRoute is '/random-page' in execution_table.
At which step does the routeExists variable become false?
AAfter Step 3
BAfter Step 1
CAfter Step 2
DAfter Step 4
💡 Hint
Look at variable_tracker for routeExists values after each step.
If you add a new page for '/blog', what changes in the execution_table when requesting '/blog'?
ArouteExists will be false and NotFound component renders
BrouteExists will be true and the Blog page component renders
CNo change, NotFound component always renders
DExecution will stop immediately
💡 Hint
Think about how Next.js checks routes and renders components based on routeExists.
Concept Snapshot
Next.js shows a 404 page when a route is not found.
Create a not-found.js file exporting a React component.
Next.js automatically renders this for missing routes.
User sees a friendly 'Page Not Found' message.
This improves user experience on bad URLs.
Full Transcript
When a user visits a URL in a Next.js app, the framework checks if the route exists. If it does, Next.js renders the matching page component. If the route does not exist, Next.js renders the custom 404 page defined in not-found.js. This page usually shows a message like '404 - Page Not Found' to inform the user. The code example shows a simple React component for the 404 page. The execution table traces requests to different URLs, showing which component renders and what the user sees. Variables track the requested route, whether it exists, and which component is rendered. Key moments clarify why Next.js renders the NotFound component for missing routes and how to customize the 404 page. The visual quiz tests understanding of which component renders for given routes and how adding pages affects route existence. This helps beginners see how Next.js handles not-found pages step-by-step.

Practice

(1/5)
1. What is the purpose of a not-found.tsx file in a Next.js app directory?
easy
A. To display a custom message when a user visits a page that does not exist.
B. To define the main homepage of the website.
C. To handle user authentication and login.
D. To store global CSS styles for the app.

Solution

  1. Step 1: Understand the role of not-found.tsx

    The not-found.tsx file is used to show a special page when a user tries to visit a URL that does not match any existing page.
  2. Step 2: Compare with other options

    The other options describe unrelated features: defining the main homepage, handling user authentication and login, and storing global CSS styles, which are not handled by not-found.tsx.
  3. Final Answer:

    To display a custom message when a user visits a page that does not exist. -> Option A
  4. Quick Check:

    Not-found page = Custom message for missing pages [OK]
Hint: Not-found page shows when URL has no matching page [OK]
Common Mistakes:
  • Confusing not-found.tsx with homepage or layout files
  • Thinking it handles authentication
  • Assuming it manages styles
2. Which of the following is the correct location for the not-found.tsx file in a Next.js 14 app using the App Router?
easy
A. Inside the pages directory
B. Inside the components directory
C. Inside the app directory
D. Inside the public directory

Solution

  1. Step 1: Recall Next.js 14 App Router structure

    In Next.js 14 with the App Router, special files like not-found.tsx belong inside the app directory to define routes and error pages.
  2. Step 2: Eliminate incorrect directories

    The pages directory is for the Pages Router, public is for static assets, and components is for reusable UI parts, not special route files.
  3. Final Answer:

    Inside the app directory -> Option C
  4. Quick Check:

    App Router special files go in app directory [OK]
Hint: App Router files like not-found.tsx go in app folder [OK]
Common Mistakes:
  • Placing not-found.tsx in pages directory (old router)
  • Putting it in public or components folders
  • Confusing app and pages directories
3. Given this not-found.tsx component in Next.js:
export default function NotFound() {
  return (
    <main>
      <h1>Page Not Found</h1>
      <p>Sorry, we couldn't find that page.</p>
    </main>
  )
}

What will a user see if they visit a non-existent page?
medium
A. A blank page with no content
B. The message: 'Page Not Found' and 'Sorry, we couldn't find that page.'
C. An error message from the server
D. The homepage content

Solution

  1. Step 1: Read the JSX returned by NotFound component

    The component returns a main section with a heading 'Page Not Found' and a paragraph apologizing for missing page.
  2. Step 2: Understand Next.js behavior on missing pages

    When a user visits a page that does not exist, Next.js renders this not-found.tsx component, showing the messages inside.
  3. Final Answer:

    The message: 'Page Not Found' and 'Sorry, we couldn't find that page.' -> Option B
  4. Quick Check:

    NotFound component content = shown on missing pages [OK]
Hint: NotFound component content displays on missing pages [OK]
Common Mistakes:
  • Expecting server error instead of custom message
  • Thinking homepage shows instead
  • Assuming blank page appears
4. You wrote a not-found.tsx file but Next.js still shows the default 404 page. What is the most likely mistake?
medium
A. The not-found.tsx file has a syntax error in the JSX.
B. You named the file NotFound.tsx with uppercase letters.
C. You forgot to restart the Next.js development server.
D. The not-found.tsx file is placed inside the components folder instead of app.

Solution

  1. Step 1: Check file placement rules for not-found.tsx

    The not-found.tsx file must be inside the app directory to be recognized by Next.js as the custom 404 page.
  2. Step 2: Analyze other options

    Syntax errors cause build failures, restarting server is good but not required for recognition, and filename is case sensitive but must be exactly not-found.tsx (lowercase). Placing it in components means Next.js ignores it.
  3. Final Answer:

    The not-found.tsx file is placed inside the components folder instead of app. -> Option D
  4. Quick Check:

    File location matters for Next.js to detect not-found page [OK]
Hint: Put not-found.tsx inside app folder, not components [OK]
Common Mistakes:
  • Placing not-found.tsx outside app directory
  • Ignoring case sensitivity of filename
  • Assuming server restart fixes detection
5. You want to create a helpful not-found page in Next.js that includes a link back to the homepage. Which code snippet correctly implements this inside not-found.tsx?
hard
A. export default function NotFound() { return (

Page Not Found

Sorry, we couldn't find that page.

Go back home
) }
B. export default function NotFound() { return (

Page Not Found

Sorry, we couldn't find that page.

Go back home
) }
C. export default function NotFound() { return (

Page Not Found

Sorry, we couldn't find that page.

Go back home
) }
D. export default function NotFound() { return (

Page Not Found

Sorry, we couldn't find that page.

Go back home
) }

Solution

  1. Step 1: Identify correct link usage in Next.js not-found page

    In Next.js, the not-found.tsx page can use a normal <a> tag with href to link back to the homepage. The Link component requires import and is used differently.
  2. Step 2: Check each option's link syntax

    The snippet with <a href="/"> is valid JSX/HTML and requires no imports. Snippets with <Link href="/"> reference undefined Link (missing import { Link } from 'next/link';). Snippets with to="/" use invalid attribute ('to' is for React Router; use href).
  3. Final Answer:

    Using <a href="/">Go back home</a> -> Option A
  4. Quick Check:

    Use <a href="/"> for links in not-found.tsx [OK]
Common Mistakes:
  • Using <Link> without import or proper usage
  • Using 'to' attribute instead of 'href'
  • Forgetting to provide a link back to home
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