Performance: Error handling with error.tsx
This affects page load speed and interaction responsiveness by controlling how errors are caught and displayed without full page reloads.
export default function Error({ error, reset }) { return ( <div role="alert"> <p>Something went wrong: {error.message}</p> <button onClick={() => reset()}>Try again</button> </div> ); } // error.tsx catches errors and allows recovery without reload
export default function Page() { if (someError) { throw new Error('Oops!'); } return <div>Content</div>; } // No error boundary or error.tsx used
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No error.tsx, error causes full reload | High (full page reload creates many DOM nodes) | Multiple (full re-layout triggered) | High (browser repaints entire page) | [X] Bad |
| Using error.tsx with fallback UI | Low (only error UI nodes rendered) | Single (only error UI layout) | Low (small repaint area) | [OK] Good |
error.tsx file in a Next.js route?error.tsxerror.tsx.error.tsx for Next.js?error.tsx. export default function Error() { return Error occurred
; } usesexport default function, which is correct.Error occurred
; } lacks export, so it won't be recognized. export function Error() { returnError occurred
; } exports a named function, not default. const Error = () => {Error occurred
}; export default Error; misses a return statement inside the arrow function, causing no output.Error occurred
; } [OK]error.tsx component, what will be rendered when an error occurs?export default function Error() {
return (
<section role="alert">
<h1>Oops! Something went wrong.</h1>
<button onClick={() => window.location.reload()}>Try again</button>
</section>
);
}error.tsx but it does not show the error UI when an error happens:function Error() {
return <p>Error!</p>;
}
export default Error;error.tsx file to be in the route folder to catch errors there. If misplaced or misnamed, it won't work.error.tsx and placed in the route folder [OK]error.tsx component. Which change best helps screen reader users?role="alert" tells screen readers to announce the error immediately, improving accessibility.role="alert" to the container element [OK]Pass the test and this pattern moves unknown