Part 07 / Topic 4 of 7
loading.tsx: a Suspense boundary for a whole route segment
A second route, harness/app/ops/orders/page.tsx, lists every delayed order with no <Suspense> anywhere in its own file:
export default async function AllDelayedOrdersPage() {
const orders = await getDelayedOrders();
return (
<main style={{ fontFamily: "system-ui", padding: "2rem", maxWidth: 560 }}>
<h1>All delayed orders</h1>
<ul id="all-delayed-orders">
{orders.map((order) => (
<li key={order.id}>{order.id} — {order.partner} ({order.city}), ETA {order.etaMinutes} min</li>
))}
</ul>
</main>
);
}
A sibling file in the same folder, loading.tsx, is the whole fix:
export default function Loading() {
return (
<main style={{ fontFamily: "system-ui", padding: "2rem", maxWidth: 560 }}>
<div aria-hidden style={{ height: "1.75rem", width: "14rem", background: "#e5e7eb", marginBottom: "1rem", borderRadius: 4 }} />
{[0, 1, 2].map((row) => (
<div key={row} aria-hidden style={{ height: "1.1rem", width: "100%", background: "#e5e7eb", marginBottom: "0.5rem", borderRadius: 4 }} />
))}
</main>
);
}
No import, no JSX reference to Loading anywhere — reading straight from the installed package's own loading.js reference: "loading.js will be nested inside layout.js. It will automatically wrap the page.js file... in a <Suspense> boundary." This is page-level streaming: there is nothing worth showing in AllDelayedOrdersPage until its one data read resolves, so the whole page gets a fallback instead of one widget inside it.
Verified for real, same stream-observer script:
$ node examples/05-stream-observer.mjs http://localhost:PORT/ops/orders
chunk 1 at +29ms (2059 bytes)
chunk 2 at +32ms (6822 bytes)
chunk 3 at +35ms (5958 bytes)
chunk 4 at +1232ms (672 bytes)
chunk 5 at +1232ms (1655 bytes)
chunk 6 at +1233ms (14 bytes)
---summary---
status: 200
total chunks: 6
first chunk has h1: false
id="all-delayed-orders" arrived at: +1232ms
The skeleton (no <h1> tag — loading.tsx never writes one) ships in the first three chunks within ~35ms; the real <li> rows (id="all-delayed-orders") replace it roughly 1200ms later — exactly getDelayedOrders's simulated delay, proving the swap really is driven by when the data resolves, not by anything else.
Skeleton dimensions matter. The three skeleton bars above are sized close to the real <li> rows they stand in for. If a fallback is a single short line and the real content is three paragraphs, the page visibly jumps when the swap happens — a real Cumulative Layout Shift, not a cosmetic nitpick. Match the fallback's height to the content's, not just its presence.
Full files: examples/03-orders-page-with-loading.tsx, examples/04-orders-loading-skeleton.tsx.
Done here? The practice set for Part 7 — including the solutions — is on the part page.