Lesson 180 of 296
Streaming, loading.tsx and error.tsx
What you will learn
- What streaming is and why it helps
- loading.tsx
- Suspense for parts of a page
- error.tsx and global-error.tsx
- not-found.tsx
- Designing good loading states
On this page
The problem
A dashboard needs three pieces of data: the user (fast), recent orders (medium) and a sales report (slow, 3 seconds). If the page waits for everything, the user sees nothing for 3 seconds.
Streaming
Streaming means the server sends the page in pieces: it sends the ready parts first (layout, header, fast data) and keeps the connection open, sending each slow part as soon as it is ready. The browser shows each piece immediately. React and Next.js do this with Suspense.
loading.tsx
Put loading.tsx in a route folder, and Next.js automatically wraps the page in Suspense with it as the fallback:
export default function Loading() {
return (
<div aria-busy="true" aria-live="polite">
<p>Loading your dashboard...</p>
</div>
);
}- The layout shows instantly; the loading UI appears in place of the page until it is ready.
- Navigation feels instant because the user sees a response immediately.
- Use skeletons (grey shapes matching the final layout) rather than a lone spinner, so the layout does not jump when content arrives (CLS, Lesson 86).
Suspense for sections
For finer control, wrap each slow section in its own Suspense, so fast sections do not wait for slow ones:
import { Suspense } from "react";
async function SalesReport() {
await new Promise((resolve) => setTimeout(resolve, 3000));
return <section><h2>Sales</h2><p>£12,400 this week</p></section>;
}
async function RecentOrders() {
await new Promise((resolve) => setTimeout(resolve, 500));
return <section><h2>Recent orders</h2><p>3 new orders</p></section>;
}
export default function DashboardPage() {
return (
<>
<h1>Dashboard</h1>
<Suspense fallback={<p>Loading orders...</p>}>
<RecentOrders />
</Suspense>
<Suspense fallback={<p>Loading sales report...</p>}>
<SalesReport />
</Suspense>
</>
);
}The heading shows at once, orders after half a second, and the report after 3 seconds. Both sections load in parallel (no waterfall). Streaming HTML also helps SEO: the content is still in the final HTML.
error.tsx
error.tsx catches errors thrown while rendering that route segment (an error boundary, Lesson 173, created for you). It must be a Client Component:
"use client";
import { useEffect } from "react";
export default function DashboardError({
error,
reset
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
console.error(error);
}, [error]);
return (
<div role="alert">
<h2>Something went wrong loading the dashboard.</h2>
<button type="button" onClick={() => reset()}>Try again</button>
</div>
);
}reset()tries to render the segment again.- In production, server error messages are hidden from the browser for security; you get a
digestID that matches the server logs. - The error boundary sits inside the layout of the same folder, so the layout (navigation) still works. Errors in a layout itself are caught by the parent folder's
error.tsx. global-error.tsxin theappfolder catches errors in the root layout. It must include its own<html>and<body>.
not-found.tsx
Shown when notFound() is called (Lesson 176) or when no route matches. Returns a 404 status, which matters for SEO.
import Link from "next/link";
export default function NotFound() {
return (
<div>
<h2>Page not found</h2>
<p>We could not find that lesson.</p>
<Link href="/lessons">Back to all lessons</Link>
</div>
);
}The order of special files
For one route folder, Next.js nests them like this:
<Layout>
<ErrorBoundary fallback={<Error />}>
<Suspense fallback={<Loading />}>
<ErrorBoundary fallback={<NotFound />}>
<Page />
</ErrorBoundary>
</Suspense>
</ErrorBoundary>
</Layout>Knowing this order answers questions like "why does my layout still show when the page errors?"
How to say it in an interview
"Streaming sends the page in pieces: the shell and fast parts arrive first and slow parts stream in when ready, using Suspense. loading.tsx wraps a route in Suspense automatically, and I wrap individual slow sections in their own Suspense with skeleton fallbacks so they load in parallel without layout shift. error.tsx is a client error boundary for the segment with a reset function, nested inside that folder's layout, and global-error.tsx covers the root layout. notFound() renders not-found.tsx with a 404 status."
Key words
| Word | Meaning |
|---|---|
| Streaming | Sending the page to the browser in pieces as they become ready |
| loading.tsx | Automatic Suspense fallback for a route |
| Skeleton | A placeholder shaped like the content |
| error.tsx | An error boundary for a route segment |
| digest | An ID linking a hidden production error to the server logs |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.