Performance: Not-found page handling
This affects the page load speed and user experience when a user navigates to a non-existent route.
import { notFound } from 'next/navigation'; export default function Page() { // Next.js built-in to trigger 404 notFound(); return null; }
export default function Page() { return <div>Page not found</div>; } // No special 404 handling, no status code set
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side 404 rendering without status | Moderate (full page DOM) | Multiple (full layout) | High (full paint) | [X] Bad |
| Server-side 404 with notFound() | Minimal (error page DOM) | Single reflow | Low (simple paint) | [OK] Good |
not-found.tsx file in a Next.js app directory?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.not-found.tsx.not-found.tsx file in a Next.js 14 app using the App Router?not-found.tsx belong inside the app directory to define routes and error pages.pages directory is for the Pages Router, public is for static assets, and components is for reusable UI parts, not special route files.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>
)
}not-found.tsx component, showing the messages inside.not-found.tsx file but Next.js still shows the default 404 page. What is the most likely mistake?not-found.tsx file must be inside the app directory to be recognized by Next.js as the custom 404 page.not-found.tsx (lowercase). Placing it in components means Next.js ignores it.not-found.tsx?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.<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).Pass the test and this pattern moves unknown