Fullstack CourseLearn by building
Back to week 3

Topic

Handle loading and errors

Definition

App Router special files such as loading.tsx and error.tsx define route-segment loading UI and recoverable error boundaries.

In simpler words

Show honest waiting UI and a nearby recovery path when a route cannot render.

Route boundaries complement, rather than replace, request states inside interactive client components.

After this you can

  • Add route-level loading and error fallbacks
  • Explain why error.tsx is client-side
  • Distinguish route failures from mutation errors

Put fallbacks near the route

loading.tsx supplies immediate feedback while a route segment streams. It should resemble the eventual content enough that the interface does not jump or mislead users.

error.tsx is a Client Component boundary. It receives an error and reset callback so users can retry a transient route-rendering failure without manually refreshing the browser.

A recoverable route error boundary

'use client';
export default function ErrorPage({ reset }: { reset: () => void }) {
  return <section><p role="alert">Could not load this page.</p><button onClick={reset}>Try again</button></section>;
}

The reset callback asks Next to retry the route segment.

Mistake: hiding a failure with null

// Wrong — user sees an unexplained blank screen
if (error) return null;

// Right
if (error) return <p role="alert">Could not load tickets. Try again.</p>;

Every error path needs visible context and a useful next step.

Handle request errors where users act

Definition

A route boundary catches rendering failures, while a Query or mutation error belongs near the list or form that initiated the request.

In simpler words

A failed submit should preserve the user’s draft and explain how to retry.

Use accessible status and alert semantics for asynchronous feedback.

Do not expose raw backend internals in a user-facing message; log diagnostic context separately.

Live playground

Error handling playground

Cause a route and a mutation failure, then compare their recovery UI.

Show skeleton while the segment streams.

Keep in mind

  • Use loading.tsx for segment-level waiting UI.
  • Make error.tsx a Client Component that offers reset.
  • Keep failed form drafts visible for correction and retry.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement is the most accurate definition of Next error handling?
Syntax2. Which code-level choice is consistent with Next error handling?
Practical3. A reviewer sees this situation. Which decision best applies Next error handling?
Logic4. What reasoning correctly explains why this approach works for Next error handling?
Concept5. Which boundary does the correct use of Next error handling preserve?
Practical6. What is the safest next step when implementing Next error handling?
Syntax7. Which implementation direction matches the rule for Next error handling?
Logic8. Which consequence follows from applying Next error handling correctly?
Concept9. Which claim about Next error handling is true in this monorepo?
Practical10. Which team practice best demonstrates Next error handling?