Skip to content

A Boundary Owns a Waiting Area

The Course
•
18 min study

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:

JSX
export default async function Page() {
  const reader = await getReader();
  const items = await getItems();
  const report = await getReport();
  return (
    <main>
      <h1>{reader.name}&apos;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.

A Boundary Owns a Waiting Area Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import { getReader, getItems, getReport } from "@/lib/library.js"; export const dynamic = "force-dynamic"; export default async function Page() { const reader = await getReader(); const items = await getItems(); const report = await getReport(); return ( <main> <h1>{reader.name}&apos;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> ); } export default function Loading() { return <p>Loading the library…</p>; } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } // Pretend database calls with fixed delays. const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getReader() { await wait(100); return { name: "Ada" }; } export async function getItems() { await wait(300); return ["Routing", "Forms", "Caching"]; } export async function getReport() { await wait(1200); return { minutes: 340, finished: 2 }; }
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.
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.

Exercise

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.

Your working space Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import { getReader, getItems, getReport } from "@/lib/library.js"; export const dynamic = "force-dynamic"; export default async function Page() { const reader = await getReader(); const items = await getItems(); const report = await getReport(); return ( <main> <h1>{reader.name}&apos;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> ); } export default function Loading() { return <p>Loading the library…</p>; } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } // Pretend database calls with fixed delays. const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getReader() { await wait(100); return { name: "Ada" }; } export async function getItems() { await wait(300); return ["Routing", "Forms", "Caching"]; } export async function getReport() { await wait(1200); return { minutes: 340, finished: 2 }; }
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

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
JSX
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}&apos;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.

Was this article helpful?

Your response is saved on this device.