A loading file covers the whole page. Often only part of a page is slow. Then the rest can show at once, and the slow part can follow.
npm install and npm run dev. Press Reload in the address bar, and watch: the heading first, the streak after 0.4 seconds, the suggestions after 1.5.
Suspense in a page
<Suspense fallback={<p className="muted">Loading streak…</p>}>
<Streak />
</Suspense>Streak and Suggestions are async server components that load their own
data. Each sits inside its own <Suspense>, from React. The page itself doesn't
await anything, so it's ready at once, with each boundary showing its
fallback. As each component finishes on the server, its HTML is streamed
to the browser and replaces its fallback, in whatever order they finish:
async function Streak() {
const days = await getStreak();
return <p>Streak: {days} days</p>;
}
async function Suggestions() {
const suggestions = await getSuggestions();
return (
<ul>
{suggestions.map((s) => <li key={s}>{s}</li>)}
</ul>
);
}This is what loading.jsx does for a whole page, at a finer grain, and you
choose where the boundaries go.
Streaming and the status code
An HTTP response starts with its status code, before any of the page. Once
Next.js has streamed the first part, the status has already gone out as
200. So if a part inside a <Suspense> calls notFound() (Day 3), the
reader still sees the not-found page, but the status stays 200. Next.js
adds a noindex tag so search engines skip it. Call notFound() before the
first boundary, at the top of the page, when the status matters.
Predict
Put Streak, the Suggested next heading and Suggestions inside one
<Suspense>, with the fallback Loading…. When does the streak appear?
Check your prediction
After 1.5 seconds, together with the suggestions. A boundary shows its content only when everything inside it is ready. Put parts that should arrive separately in separate boundaries; put parts that belong together in one, so the page doesn't jump about.
A third part
Between the streak and Suggested next, add a part that shows 240 minutes this week after 0.9 seconds, with its own fallback Loading minutes….
npm install and npm run dev. Hint 1: the idea
A separate slow component needs a separate waiting boundary if it should arrive independently.
Hint 2: a first step
Write an async Minutes component shaped like Streak, but instead of
loading data, have it wait 900 ms with
await new Promise((resolve) => setTimeout(resolve, 900));. Render it between
the streak and Suggested next first, with no <Suspense>, and press
Reload: the whole dashboard now waits 0.9 seconds. What does it need to
arrive on its own?
Show one solution
async function Minutes() {
await new Promise((resolve) => setTimeout(resolve, 900));
return <p>240 minutes this week</p>;
}<Suspense fallback={<p className="muted">Loading minutes…</p>}>
<Minutes />
</Suspense>A <Suspense> waits only for what's inside it, so the minutes (0.9 s) and
the suggestions (1.5 s) each replace their own fallback. Putting <Minutes />
inside the suggestions boundary would also work, but then both would appear
together at 1.5 seconds, and with no boundary at all it holds up the whole
page.
Completion is saved in this browser only. It does not measure mastery.