Streaming & Suspense
Next.js streaming suspense: stream a page in parts when data is slow, with Suspense boundaries, loading.tsx and parallel data fetching.
On this page
1. What are streaming and Suspense?
Suspense is a React component that says "show this fallback until the component inside is ready". Next.js watches these boundaries on the server and streams the HTML in chunks.
2. Why does it matter?
If one part of a page takes 3 seconds, then without streaming the whole page stays blank for 3 seconds.
| Without streaming | With streaming |
|---|---|
| Blank screen until the slowest query finishes | Header and layout show immediately |
| One slow widget blocks the whole page | Each widget arrives when it is ready |
| The user thinks the site is frozen | A skeleton shows progress |
| Higher Time to First Byte | The first byte arrives sooner |
3. Where is it used?
| Page | What to stream |
|---|---|
| Dashboard | Each chart and stats card in its own boundary |
| Product page | Reviews and recommendations later |
| Blog post | The comments section later |
| Search results | The results list with a skeleton |
| Cache Components page | Every dynamic (request time) part |
4. How does it work?
There are two ways to stream:
| Way | Scope |
|---|---|
loading.tsx | The whole page segment in one boundary |
<Suspense> | You choose the small parts yourself |
loading.tsx actually wraps the page in Suspense for you. If you want more control, add Suspense yourself.
5. Code example
A dashboard whose widgets stream separately
import { Suspense } from "react";
async function Revenue() {
const res = await fetch("https://api.example.com/revenue");
const data = await res.json();
return <p>Revenue: {data.total}</p>;
}
async function LatestOrders() {
const res = await fetch("https://api.example.com/orders?limit=5");
const orders: { id: string; amount: number }[] = await res.json();
return (
<ul>
{orders.map((order) => (
<li key={order.id}>{order.amount}</li>
))}
</ul>
);
}
export default function DashboardPage() {
return (
<main>
<h1>Dashboard</h1>
<Suspense fallback={<p>Loading revenue...</p>}>
<Revenue />
</Suspense>
<Suspense fallback={<p>Loading orders...</p>}>
<LatestOrders />
</Suspense>
</main>
);
}Parallel fetching to avoid a waterfall
async function getUser(id: string) {
return fetch(`https://api.example.com/users/${id}`).then((r) => r.json());
}
async function getPosts(id: string) {
return fetch(`https://api.example.com/users/${id}/posts`).then((r) => r.json());
}
export default async function ProfilePage({ params }: PageProps<"/users/[id]">) {
const { id } = await params;
const [user, posts] = await Promise.all([getUser(id), getPosts(id)]);
return (
<h1>
{user.name} has {posts.length} posts
</h1>
);
}How does the code work?
| Part | Meaning |
|---|---|
async function Revenue | An async Server Component that fetches its own data |
<Suspense fallback=...> | Shows the fallback until the data arrives, then streams the real UI |
| Two separate boundaries | Revenue and orders do not wait for each other |
<h1>Dashboard</h1> | Outside any boundary, so it shows immediately |
Promise.all | Starts both requests together, no waterfall |
Interview tip
"Does streaming hurt SEO?" No. Streamed content is still HTML coming from the server in the same HTTP response, and crawlers like Google can read it.
6. Common mistakes
Mistake: Putting Suspense inside the async component
Why? The boundary must be above the component that suspends. Placing it inside does not work.
Better: Add Suspense in the parent and put the slow async component inside it.
Mistake: Awaiting one after another when the requests are independent
Why? Each request waits for the previous one, so the total time becomes the sum of all of them.
Better: Run independent requests together with Promise.all, or put them in separate Suspense boundaries.
Mistake: Wrapping the whole page in one boundary
Why? The slowest part blocks everything else too.
Better: Give every independent slow part its own Suspense boundary.
Mistake: Making the fallback a completely different size from the real UI
Why? The layout shifts when the content arrives and your CLS score gets worse.
Better: Keep the size and shape of the skeleton close to the real UI.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | React Suspense, Loading, Error & Not Found UI, Promises |
| Alongside | Rendering Strategies, Server & Client Components |
| After | Data Fetching, Cache Components, Performance |
Final quick revision
| Point | Detail |
|---|---|
| Streaming | HTML in parts, in a single response |
| Boundary | <Suspense fallback=...> |
| Page level | loading.tsx |
| Benefit | Faster first paint, independent widgets |
| Avoiding waterfalls | Promise.all or separate boundaries |
| Cache Components | Request time data must be inside Suspense |