Skip to content

Streaming Parts of a Page

The Course
•
20 min study

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.

A dashboard that streams Next.js · 4 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import { Suspense } from "react"; import { getStreak, getSuggestions } from "@/lib/dashboard.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; 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> ); } export default function DashboardPage() { return ( <main> <h1>Dashboard</h1> <Suspense fallback={<p className="muted">Loading streak…</p>}> <Streak /> </Suspense> <h2>Suggested next</h2> <Suspense fallback={<p className="muted">Loading suggestions…</p>}> <Suggestions /> </Suspense> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getStreak() { await wait(400); return 4; } export async function getSuggestions() { await wait(1500); return ["Review layouts", "Try a dynamic route"]; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

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

JSX
<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:

JSX
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.

Exercise

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….

Your working space Next.js · 4 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import { Suspense } from "react"; import { getStreak, getSuggestions } from "@/lib/dashboard.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; 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> ); } export default function DashboardPage() { return ( <main> <h1>Dashboard</h1> <Suspense fallback={<p className="muted">Loading streak…</p>}> <Streak /> </Suspense> <h2>Suggested next</h2> <Suspense fallback={<p className="muted">Loading suggestions…</p>}> <Suggestions /> </Suspense> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getStreak() { await wait(400); return 4; } export async function getSuggestions() { await wait(1500); return ["Review layouts", "Try a dynamic route"]; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
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
JSX
async function Minutes() {
  await new Promise((resolve) => setTimeout(resolve, 900));
  return <p>240 minutes this week</p>;
}
JSX
<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.

Was this article helpful?

Your response is saved on this device.