What is the purpose of a not-found page in Next.js?
A not-found page shows a friendly message when a user tries to visit a page that does not exist. It helps users understand they reached a wrong or missing page.
Click to reveal answer
beginner
How do you create a custom 404 page in Next.js?
Create a file named app/not-found.tsx or pages/404.tsx with a React component that returns the UI you want to show when a page is not found.
Click to reveal answer
intermediate
What React hook can you use inside app/not-found.tsx to navigate back to a valid page?
You can use the useRouter hook from next/navigation to programmatically navigate users back to a valid page, like the homepage.
Click to reveal answer
beginner
What happens if you do not create a custom 404 page in Next.js?
Next.js will show a default 404 page with a simple message. It works but is not styled or customized for your app's look and feel.
Click to reveal answer
advanced
How can you trigger the not-found page programmatically in Next.js server components?
You can call the notFound() function from next/navigation inside a server component or server action to show the not-found page when data is missing.
Click to reveal answer
Where do you place a custom 404 page in a Next.js app router project?
AIn the <code>app/not-found.tsx</code> file
BIn the <code>pages/404.tsx</code> file
CIn the <code>public/404.html</code> file
DIn the <code>components/NotFound.tsx</code> file
✗ Incorrect
In Next.js app router, the custom not-found page goes in app/not-found.tsx.
What function do you import to programmatically show the not-found page in Next.js server components?
A<code>notFound()</code>
B<code>useRouter()</code>
C<code>redirect()</code>
D<code>useNotFound()</code>
✗ Incorrect
The notFound() function from next/navigation triggers the not-found page.
What is the default behavior if no custom 404 page is created in Next.js?
AThe app crashes
BA default Next.js 404 page shows
CA blank page shows
DThe homepage loads instead
✗ Incorrect
Next.js shows a default 404 page if no custom one is provided.
Which hook helps navigate users back from a not-found page in Next.js client components?
A<code>useState()</code>
B<code>useEffect()</code>
C<code>useRouter()</code>
D<code>useNotFound()</code>
✗ Incorrect
useRouter() from next/navigation allows navigation control.
In Next.js, what file name is used for a custom 404 page in the pages directory (not app router)?
Aerror.tsx
Bnot-found.js
C404.js
D404.tsx
✗ Incorrect
In the pages directory, the custom 404 page is named 404.tsx.
Explain how to create and use a custom not-found page in a Next.js app router project.
Think about file location, component, and navigation helpers.
You got /4 concepts.
Describe what happens when a user visits a missing page in Next.js without a custom 404 page.
Focus on default behavior and user experience.
You got /4 concepts.
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
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.
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.
Final Answer:
To display a custom message when a user visits a page that does not exist. -> Option A
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
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.
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.
Final Answer:
Inside the app directory -> Option C
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
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.
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.
Final Answer:
The message: 'Page Not Found' and 'Sorry, we couldn't find that page.' -> Option B
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
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.
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.
Final Answer:
The not-found.tsx file is placed inside the components folder instead of app. -> Option D
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?
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.
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).