Quick answer
Use loading.tsx for a route segment’s loading fallback and error.tsx for unexpected errors in its child rendering tree. Use narrower Suspense boundaries when only one region is slow. Return expected field errors or missing-content outcomes deliberately rather than throwing every unsuccessful result. Recovery must describe what it actually does: clearing an error state, refetching server content, and repeating a write are different operations.
Practical implementation steps
1. Place the boundary above awaited work
A parent that awaits a slow request before returning a Suspense boundary cannot stream that boundary’s fallback while waiting. Move the awaited work into the child below it. Use a fallback that reflects the region’s size and purpose, and avoid making the whole page blink for a small optional panel. Slow layout work can block before a segment loading file applies, so inspect the actual hierarchy.
2. Map which failures each boundary catches
An error file is a Client Component and catches errors below its segment boundary. It does not catch errors in its own segment’s layout, which sits above it; a parent boundary or global-error handles that scope. Root-level global error UI supplies its own html and body. Event-handler failures and background callbacks need their own error handling rather than an assumption that rendering boundaries catch them.
3. Match recovery to the installed version
This repository uses Next.js 16.2.10. Its error boundary props include reset and unstable_retry; current documentation records retry becoming stable in 16.3. reset clears the boundary error state without refetching server content, while the retry path refetches and rerenders children. Do not copy a newer retry signature into an older application or promise that reset refreshes an upstream read.
4. Keep status and user feedback accurate
Once a streamed response has sent headers, a later missing-content result cannot retroactively change its HTTP status. Validate existence or access before streaming when the status contract requires it. Avoid exposing raw server exceptions to users; use a recoverable explanation and a diagnostic reference. A retry button should not repeatedly trigger a non-idempotent business mutation as part of rendering.
Implementation example
This error component targets the installed 16.2 API; unstable_retry is an explicitly unstable version-specific prop. The label promises a reload of content, rather than merely clearing local error state. Do not treat the example as a write retry policy. Keep mutation status separate so a lost response cannot cause an accepted booking to be created again.
// app/reports/error.tsx — Next.js 16.2
"use client";
export default function ReportsError({
error, unstable_retry,
}: {
error: Error & { digest?: string };
unstable_retry: () => void;
}) {
return (
<section aria-labelledby="reports-error">
<h2 id="reports-error">Reports could not load</h2>
<p>Reload the content, or return later.</p>
{error.digest && <p>Reference: {error.digest}</p>}
<button type="button" onClick={unstable_retry}>Reload content</button>
</section>
);
}
Tradeoffs and alternatives
A segment-wide fallback is simple; separate boundaries preserve useful content when optional regions are slow or fail. Too many tiny skeletons can make the interface harder to understand. An expected empty result may need a normal empty state instead of an error boundary. Choose the unit of recovery from what the user can still do, not from the number of API calls.
Common mistakes
Avoid fallback placement after a parent await, layout failures assumed to be caught by the same segment’s error file, raw server message display, and reset described as a server refetch. Also avoid checking only successful client navigation: direct loads, missing records, delayed data, and rejected reads can reveal different status and recovery behavior.
Production checklist
- Awaited work sits below its loading boundary.
- Layout and root errors have the appropriate parent scope.
- Expected failures use meaningful normal states.
- Recovery props match the installed Next.js version.
- Status-sensitive checks occur before headers stream.
FAQ
Does error.tsx catch a click-handler exception?
Not as a general event-handler policy. Handle expected interaction failures in the handler or action result; use a rendering boundary for failures in the rendered subtree.
Should every failed read show a retry button?
Only when retry can plausibly recover and its operation is safe. Denied access, removed content, and invalid input usually need different next steps.
Next step
Test the hierarchy with one slow region, one missing record, and one unexpected rendering failure. Confirm the remaining interface stays useful and the recovery action matches its label and version. Inspect direct-response status separately from what the browser eventually displays.
Primary sources
- Next.js: loading convention
- Next.js: error convention and version history
- Next.js: Expected and unexpected errors
Related guides and examples
Need this implemented in a real product?
React and Next.js applications with deliberate rendering, reliable data flows, and a clear path to production.