Performance: Not-found.tsx customization
This affects the page load speed and user experience when a user navigates to a non-existent route, impacting the Largest Contentful Paint (LCP) and Interaction to Next Paint (INP).
Jump into concepts and practice - no test required
export default function NotFound() { return <div><h1>Page Not Found</h1><p>Sorry, we couldn't find that page.</p></div>; }
export default async function NotFound() { const data = await fetch('https://api.example.com/slow-endpoint').then(res => res.json()); return <div>{data.message}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking data fetch in NotFound | Moderate (due to data rendering) | 1+ reflows triggered by delayed content | High paint cost due to delayed render | [X] Bad |
| Static simple NotFound component | Low (minimal DOM nodes) | Single reflow | Low paint cost with immediate render | [OK] Good |
not-found.tsx file in a Next.js app?not-found.tsxnot-found.tsx [OK]not-found.tsx?export default function ComponentName() { ... }.export default function [OK]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>
);
}not-found.tsx code snippet:export default function NotFound() {
return (
<div>
<h1>Page Not Found</h1>
<a href="/">Go Home</a>
</div>
)
}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?window.navigator.userAgent works only on client side, so use React hooks like useEffect.not-found.tsx. CSS media queries only style but don't change content. Multiple files for one route is invalid.useEffect with window.navigator.userAgent to detect device and conditionally render -> Option B