Streaming and partial rendering let your web page show parts of content as soon as they are ready. This makes the page feel faster and smoother for users.
Streaming and partial rendering in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NextJS
export default async function Page() { const data = await fetchData(); return ( <> <Header /> <MainContent data={data} /> <Footer /> </> ); }
Use async functions in server components to fetch data before rendering.
Next.js automatically streams the HTML to the browser as parts become ready.
Examples
NextJS
export default async function Page() { const user = await fetchUser(); return ( <> <UserProfile user={user} /> <Comments /> </> ); }
Suspense lets you show a loading state while MainContent loads.NextJS
import React, { Suspense } from 'react'; export default function Page() { return ( <> <Header /> <Suspense fallback={<Loading />}> <MainContent /> </Suspense> </> ); }
Sample Program
This Next.js component shows a heading immediately. The main content waits 2 seconds to fetch data, showing a loading message meanwhile. Once data arrives, it replaces the loading message.
NextJS
import React, { Suspense } from 'react'; async function fetchData() { return new Promise(resolve => setTimeout(() => resolve('Hello from server!'), 2000)); } function Loading() { return <p>Loading content...</p>; } async function MainContent() { const message = await fetchData(); return <p>{message}</p>; } export default function Page() { return ( <main> <h1>Welcome to Streaming Demo</h1> <Suspense fallback={<Loading />}> <MainContent /> </Suspense> </main> ); }
Important Notes
Streaming improves user experience by showing parts of the page early.
Use Suspense to handle loading states gracefully.
Server components in Next.js support async data fetching and streaming by default.
Summary
Streaming sends page parts to the browser as soon as they are ready.
Partial rendering lets you show loading states for slow parts.
Next.js makes streaming easy with async server components and Suspense.
Practice
1. What is the main benefit of streaming in Next.js when rendering pages?
easy
Solution
Step 1: Understand streaming concept in Next.js
Streaming means sending parts of the page to the browser immediately when they are ready, not waiting for the whole page.Step 2: Compare options with streaming behavior
It sends parts of the page to the browser as soon as they are ready. matches this behavior exactly, while others describe delaying or caching, which are not streaming.Final Answer:
It sends parts of the page to the browser as soon as they are ready. -> Option DQuick Check:
Streaming = send parts early [OK]
Hint: Streaming means show parts early, not wait for all [OK]
Common Mistakes:
- Thinking streaming waits for full page
- Confusing streaming with client caching
- Assuming streaming disables server rendering
2. Which of the following is the correct way to use
Suspense for partial rendering in Next.js?easy
Solution
Step 1: Recall Suspense usage pattern
Suspense wraps the async component and uses the fallback prop to show a loading state while waiting.Step 2: Match correct syntax
<Suspense fallback=<Loading />><AsyncComponent /></Suspense> correctly wraps AsyncComponent inside Suspense with fallback. Others misuse fallback or component placement.Final Answer:
<Suspense fallback=<Loading />><AsyncComponent /></Suspense> -> Option CQuick Check:
Suspense wraps async with fallback [OK]
Hint: Suspense wraps async component with fallback prop [OK]
Common Mistakes:
- Placing fallback inside async component
- Not wrapping async component with Suspense
- Using fallback incorrectly as a child
3. Given this Next.js server component code using streaming:
What will the user see first in the browser?
export default async function Page() {
const user = await fetchUser();
return (
<>
Welcome {user.name}
Loading posts...}>
</>
);
}What will the user see first in the browser?
medium
Solution
Step 1: Analyze async data fetching and rendering
The user data is awaited before rendering, so the heading with user name shows immediately.Step 2: Understand Suspense fallback behavior
The Posts component is wrapped in Suspense with a fallback, so 'Loading posts...' shows while posts load.Final Answer:
The heading with user name, then 'Loading posts...' while posts load. -> Option BQuick Check:
Await user shows heading first, Suspense fallback next [OK]
Hint: Awaited data shows first; Suspense fallback shows during async child load [OK]
Common Mistakes:
- Thinking Suspense fallback shows before awaited user data
- Assuming full page waits for all data
- Ignoring Suspense fallback usage
4. Identify the error in this Next.js streaming code snippet:
export default async function Page() {
return (
Loading...}>
);
}
function AsyncData() {
const data = fetchData();
return <p>{data.message}</p>;
}medium
Solution
Step 1: Check AsyncData function for async usage
AsyncData calls fetchData() but does not await it or mark function async, causing a promise to be rendered.Step 2: Confirm Suspense usage and fallback validity
Suspense usage is correct, and fallback can be JSX, so no error there.Final Answer:
AsyncData is not marked async and fetchData is not awaited. -> Option AQuick Check:
Async function must await async calls [OK]
Hint: Async components must await async calls inside [OK]
Common Mistakes:
- Not marking component async when using await
- Rendering unresolved promises
- Misunderstanding Suspense fallback types
5. You want to stream a page showing user info and their posts. User info loads fast, posts load slowly. How do you combine streaming and partial rendering to show user info immediately and posts with a loading fallback?
hard
Solution
Step 1: Await fast user data in Page component
Mark Page async and await user data so user info renders immediately.Step 2: Wrap slow posts component in Suspense with fallback
Wrap Posts in Suspense with a loading fallback to show partial rendering while posts load.Final Answer:
Make Page async, await user data, render user info immediately, wrap Posts in Suspense with fallback. -> Option AQuick Check:
Await fast data, Suspense slow data [OK]
Hint: Await fast data, Suspense slow parts with fallback [OK]
Common Mistakes:
- Wrapping all content in one Suspense losing immediate render
- Not awaiting fast data before rendering
- Using client fetching without Suspense for server components
