0
0
NextJSframework~8 mins

Error.tsx for route errors in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Error.tsx for route errors
MEDIUM IMPACT
This affects how quickly error pages render during route failures, impacting user experience during navigation errors.
Handling route errors with an error component
NextJS
'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>
  );
}
Static error UI renders immediately without blocking or extra data fetching, improving responsiveness.
📈 Performance GainNon-blocking render, reduces INP by 50-100 ms
Handling route errors with an error component
NextJS
'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>;
}
Running heavy synchronous logic inside the error component blocks rendering and delays error display.
📉 Performance CostBlocks rendering for 100+ ms depending on computation time, increases INP
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy synchronous logic in Error.tsxIncreased DOM nodes if rendering fetched dataMultiple reflows due to async updatesHigh paint cost due to delayed rendering[X] Bad
Static, simple Error.tsx with ARIA rolesMinimal DOM nodesSingle reflow on initial renderLow paint cost with immediate display[OK] Good
Rendering Pipeline
When a route error occurs, the Error.tsx component is rendered. If it contains heavy synchronous logic, it delays Style Calculation and Layout, blocking Paint and Composite stages.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckBlocking JavaScript execution delays Paint and Composite stages.
Core Web Vital Affected
INP
This affects how quickly error pages render during route failures, impacting user experience during navigation errors.
Optimization Tips
1Avoid heavy synchronous logic inside Error.tsx to prevent blocking rendering.
2Keep error components simple and static for fast display.
3Use ARIA roles like role="alert" and aria-live="assertive" for accessibility and immediate screen reader notification.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with heavy logic inside Error.tsx for route errors?
AIt improves Largest Contentful Paint.
BIt reduces bundle size.
CIt blocks rendering and delays error display.
DIt decreases DOM node count.
DevTools: Performance
How to check: Record a session navigating to a route that triggers Error.tsx. Look for scripting time and time to first paint of the error component.
What to look for: Long scripting blocks before paint indicate blocking logic; quick paint with minimal scripting shows good performance.