Performance: Error.tsx for route errors
This affects how quickly error pages render during route failures, impacting user experience during navigation errors.
Jump into concepts and practice - no test required
'use client'; export default function Error({error, reset}) { return ( <div role="alert" aria-live="assertive"> An error occurred. <button onClick={reset}>Please try again later.</button> </div> ); }
'use client'; export default function Error({error, reset}) { // Heavy logic inside error component let data = 0; for(let i = 0; i < 10000000; i++) { data += i; } return <div>Error occurred: {data}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous logic in Error.tsx | Increased DOM nodes if rendering fetched data | Multiple reflows due to async updates | High paint cost due to delayed rendering | [X] Bad |
| Static, simple Error.tsx with ARIA roles | Minimal DOM nodes | Single reflow on initial render | Low paint cost with immediate display | [OK] Good |
Error.tsx file in a Next.js route?Error.tsx for Next.js?Error.tsx component, what will be rendered when an error occurs?export default function Error() {
return (
<main role="alert" aria-live="assertive">
<h1>Oops! Something went wrong.</h1>
<button onClick={() => window.location.reload()}>Try Again</button>
</main>
);
}Error.tsx component code?export default function Error() {
return (
<div>
<h1>Error!</h1>
<button onClick={reloadPage}>Reload</button>
</div>
);
}
function reloadPage() {
location.reload;
}Error.tsx component by adding ARIA roles and live regions. Which of these changes is best practice?