The decision
The beginner course put slow components inside <Suspense>. Real pages rarely
start that way: they start with every await at the top of the page, because
that's the easiest place to write them. Then loading.jsx is the only waiting
area, and the whole page waits for its slowest query.
The question to ask of each await is: which part of the screen needs this?
Data the whole page needs stays at the top. Data only one part needs moves into
that part, under its own boundary.
Read, predict, run
lib/library.js has three pretend calls: getReader takes 100 ms, getItems
300 ms and getReport 1200 ms. The page awaits all three before rendering
anything, and app/loading.jsx shows Loading the library… meanwhile:
export default async function Page() {
const reader = await getReader();
const items = await getItems();
const report = await getReport();
return (
<main>
<h1>{reader.name}'s Study Library</h1>
<h2>Reading list</h2>
<ul>
{items.map((title) => (
<li key={title}>{title}</li>
))}
</ul>
<h2>Weekly report</h2>
<p>
{report.minutes} minutes, {report.finished} items finished
</p>
</main>
);
}Predict what the reader sees, and when, then press Run.
npm install and npm run dev. Check your prediction
Loading the library… for about 1.6 seconds (100 + 300 + 1200, a waterfall too), then everything at once. The heading needed only the reader, ready after 100 ms, and the reading list after 400 ms, but neither can show before the page function returns, and it can't return before the report.
The failure to avoid
A boundary only helps if the await is below it. Wrapping
<WeeklyReport report={report} /> in <Suspense> while the page still awaits
getReport() changes nothing: the page has already waited before React reaches
the boundary. The component that needs the data must fetch it itself.
Don't go too far the other way either. A boundary per paragraph makes the page jump about as pieces land. Group what the reader uses together.
Give each part its own wait
Change the page so the heading appears after about 100 ms, the reading list after about 300 ms more with Loading the list… until then, and the report when it's ready with Loading the report… until then.
npm install and npm run dev. Hint 1: the idea
The heading needs reader, so getReader stays in the page. The list and the
report each need only their own data, so each becomes an async component that
awaits it, and the page stops waiting for them.
Hint 2: a first step
Start with the report. Write async function WeeklyReport() that awaits
getReport() and returns the paragraph. Remove getReport from the page and
render <WeeklyReport /> inside a <Suspense> (import it from react). Run:
the list should now appear before the report. Then do the same for the list.
Show one solution
import { Suspense } from "react";
import { getReader, getItems, getReport } from "@/lib/library.js";
export const dynamic = "force-dynamic";
async function ReadingList() {
const items = await getItems();
return (
<ul>
{items.map((title) => (
<li key={title}>{title}</li>
))}
</ul>
);
}
async function WeeklyReport() {
const report = await getReport();
return (
<p>
{report.minutes} minutes, {report.finished} items finished
</p>
);
}
export default async function Page() {
const reader = await getReader();
return (
<main>
<h1>{reader.name}'s Study Library</h1>
<h2>Reading list</h2>
<Suspense fallback={<p>Loading the list…</p>}>
<ReadingList />
</Suspense>
<h2>Weekly report</h2>
<Suspense fallback={<p>Loading the report…</p>}>
<WeeklyReport />
</Suspense>
</main>
);
}The page now awaits only the reader, so loading.jsx shows for 100 ms. Both
children start rendering together once the page returns, so the waterfall is
gone as well: the list lands at about 400 ms and the report at about 1300 ms.
The headings sit outside the boundaries on purpose, so the reader can see the
page's shape while its data arrives.
Explain it without the code
A product page shows a name and price (fast), reviews (slow) and "customers also bought" (slower). Which awaits stay in the page, and how many boundaries would you add? Say why in one sentence each.
Going further
On a real server, notFound() inside a boundary that has already started
streaming can't change the status code any more; call it in the page, before
the first boundary, when the status matters. See the
Next.js loading and streaming reference.
Completion is saved in this browser only. It does not measure mastery.