Data Fetching and Streaming · Lesson 11 of 24
Streaming with Suspense in Next.js
Some data is slow – a recommendations engine, a reporting query, a third-party API. Without streaming, the whole page waits for the slowest part. With streaming, Next.js sends everything that's ready immediately and fills in the slow parts over the same HTTP response as they finish. You control it with React's <Suspense>.
In this lesson
- How streaming works, watched as it happens
- Several independent Suspense boundaries
loading.tsxvs<Suspense>- The static shell and where to put boundaries
A dashboard that streams
// lib/data.ts
import { connection } from "next/server";
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
export async function getProgress() {
await connection();
await wait(400);
return "12 of 31 lessons complete";
}
export async function getRecommendations() {
await connection();
await wait(1600); // the slow one
return ["Next.js", "TypeScript", "System Design"];
}
// app/page.tsx
import { Suspense } from "react";
import { getProgress, getRecommendations } from "@/lib/data";
async function Progress() {
return <p>Progress: {await getProgress()}</p>;
}
async function Recommendations() {
const items = await getRecommendations();
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}
export default function Dashboard() {
return (
<main>
<h1>Welcome back, Asha</h1> {/* static: sent immediately */}
<Suspense fallback={<p>Progress: loading…</p>}>
<Progress />
</Suspense>
<h2>Recommended for you</h2>
<Suspense fallback={<p>Finding courses for you…</p>}>
<Recommendations />
</Suspense>
</main>
);
}
Route (app) ┌ ◐ / ○ (Static) prerendered as static content ◐ (Partial Prerender) prerendered as static HTML with dynamic server-streamed content › 900 ms after opening the page Welcome back, Asha Progress: 12 of 31 lessons complete Recommended for you Finding courses for you… › fully loaded Welcome back, Asha Progress: 12 of 31 lessons complete Recommended for you • Next.js • TypeScript • System Design
One response, three moments
- 0 msthe static shell – heading, subheading and both fallbacks – was prerendered at build time and arrives at once
- ~400 msprogress finishes; its HTML is streamed and swapped in for the fallback
- ~1600 msrecommendations finish and replace their fallback
The route is ◐ Partial Prerender: the shell is static, the holes are dynamic. Each boundary resolves independently, so the fast part doesn't wait for the slow part. Streamed sections become interactive as soon as their code arrives (selective hydration).
One boundary or several?
If both components shared one <Suspense>, the progress would wait 1.6 seconds for the recommendations, and both would appear together:
<Suspense fallback={<p>Loading dashboard…</p>}>
<Progress />
<Recommendations />
</Suspense>
That can be the right choice – content that pops in piece by piece can feel jumpy. Group things that belong together visually; separate things that have very different speeds.
loading.tsx vs Suspense
loading.tsx | <Suspense> | |
|---|---|---|
| Wraps | the whole page of that route segment | exactly the components you choose |
| Best for | a page-level skeleton during navigation | independent sections with different speeds |
| Written as | a file next to page.tsx | JSX in your component |
loading.tsx is simply a <Suspense> boundary that Next.js places around your page for you. You can use both: a page skeleton from loading.tsx, and finer boundaries inside the page.
Where to put boundaries
- Put Suspense as close to the slow data as possible, so everything else stays in the instant static shell.
- Make fallbacks the same size as the final content (skeletons) to avoid layout shift.
- Don't await slow data at the top of a page or layout – that blocks everything below it. Move the await into a child component inside a boundary.
- Search engines aren't affected: for bots, Next.js waits for the full page before responding.
Practice
Try it yourself
A course page has a fast description and slow comments (1 s). Show the description immediately and a "Loading comments…" fallback until the comments arrive.
Show solution
// app/page.tsx import { Suspense } from "react"; import { connection } from "next/server"; async function Comments() { await connection(); await new Promise((r) => setTimeout(r, 1000)); return <ul><li>Ravi: Best explanation of streaming I've read.</li></ul>; } export default function CoursePage() { return ( <main> <h1>Next.js Streaming</h1> <p>Learn how Suspense streams HTML.</p> <Suspense fallback={<p>Loading comments…</p>}> <Comments /> </Suspense> </main> ); }› 500 ms after opening the page Next.js Streaming Learn how Suspense streams HTML. Loading comments… › fully loaded Next.js Streaming Learn how Suspense streams HTML. • Ravi: Best explanation of streaming I've read.
Quick check
Does streaming need several HTTP requests?
No – the slow parts arrive later in the same response.
What is in the static shell?
Everything outside Suspense boundaries that doesn't need request data, plus the fallbacks.
What does loading.tsx really do?
Wraps the route's page in a Suspense boundary with that component as the fallback.
Summary
- Streaming sends ready HTML first and slow parts later, in one response.
- Each Suspense boundary resolves independently; the rest of the page is a prerendered static shell.
- Place boundaries close to slow data, with skeleton fallbacks; loading.tsx is a page-level boundary.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.