Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Streaming and Partial Rendering with Next.js
📖 Scenario: You are building a simple Next.js app that shows a list of user comments. The comments come from a slow API. To improve user experience, you want to show the page header immediately and then stream the comments as they load one by one.
🎯 Goal: Create a Next.js app using the App Router that streams comments to the user as they load, showing the page header first and then rendering each comment as it arrives.
📋 What You'll Learn
Create a server component that fetches comments one by one with delay
Use React's async component and for await loop to stream comments
Render a page header immediately before comments load
Use Next.js 14+ App Router conventions with server components
💡 Why This Matters
🌍 Real World
Streaming and partial rendering improves user experience by showing content as soon as possible instead of waiting for all data to load.
💼 Career
Understanding streaming in Next.js and React Server Components is valuable for building fast, modern web apps that handle slow or large data efficiently.
Progress0 / 4 steps
1
Set up the comments data array
Create a constant called comments as an array with these exact strings: 'Great post!', 'Thanks for sharing.', 'Very helpful.'
NextJS
Hint
Use const comments = ['Great post!', 'Thanks for sharing.', 'Very helpful.']
2
Create a delay helper function
Add an async function called delay that takes ms and returns a Promise that resolves after ms milliseconds.
NextJS
Hint
Use return new Promise(resolve => setTimeout(resolve, ms)) inside the async delay function.
3
Create an async generator to yield comments with delay
Write an async generator function called fetchComments that loops over comments and for each comment calls await delay(1000) then yield the comment.
NextJS
Hint
Use for (const comment of comments) and yield comment after await delay(1000).
4
Create the streaming React server component page
Create a default async React server component called Page that renders a <h1> with text 'User Comments' and then uses for await (const comment of fetchComments()) to render each comment inside a <p> tag.
NextJS
Hint
Use an async IIFE inside JSX to for await over fetchComments() and render each comment inside a <p> tag.
Practice
(1/5)
1. What is the main benefit of streaming in Next.js when rendering pages?
easy
A. It caches the whole page on the client side before showing anything.
B. It delays sending the entire page until all data is loaded.
C. It disables server-side rendering completely.
D. It sends parts of the page to the browser as soon as they are ready.
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 D
Quick 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
A. } />
B. } />
C. }>
D. }>
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 C
Quick 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:
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
A. Only 'Loading posts...' until all data loads.
B. The heading with user name, then 'Loading posts...' while posts load.
C. The full page with heading and posts at once.
D. An empty page until fetchUser finishes.
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 B
Quick 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
A. AsyncData is not marked async and fetchData is not awaited.
B. Suspense cannot wrap components in Next.js.
C. fetchData should be called inside Page, not AsyncData.
D. The fallback prop must be a string, not JSX.
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 A
Quick 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
A. Make Page async, await user data, render user info immediately, wrap Posts in Suspense with fallback.
B. Render user info and posts inside one Suspense with fallback for both.
C. Fetch posts first, then user info, and render all at once without Suspense.
D. Use client-side fetching for posts and server-side for user info without Suspense.
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 A
Quick 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