0
0
NextJSframework~10 mins

Streaming long operations in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Streaming long operations
User requests page
Server starts processing
Begin streaming response
Send chunks of data as ready
Client receives and renders chunks
Streaming completes
Full page rendered
The server processes a long task and streams parts of the response to the client as they become ready, allowing the client to render progressively.
Execution Sample
NextJS
export default async function Page() {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue('Loading chunk 1...\n');
      await new Promise(r => setTimeout(r, 1000));
      controller.enqueue('Loading chunk 2...\n');
      controller.close();
    }
  });
  return new Response(stream);
}
This Next.js server component streams two chunks of text with a delay, simulating a long operation streamed to the client.
Execution Table
StepActionStream StateData SentClient Render
1User requests pageNot started
2Server starts ReadableStreamOpen
3Enqueue 'Loading chunk 1...\n'OpenLoading chunk 1...\nRenders 'Loading chunk 1...'
4Wait 1 secondOpenLoading chunk 1...\nShows first chunk
5Enqueue 'Loading chunk 2...\n'OpenLoading chunk 1...\nLoading chunk 2...\nRenders second chunk appended
6Close streamClosedLoading chunk 1...\nLoading chunk 2...\nFull content rendered
7Response completeClosedAll data sentPage fully loaded
💡 Stream closes after sending all chunks, ending the response.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
stream stateNot startedOpenOpenClosed
data sent"""Loading chunk 1...\n""Loading chunk 1...\nLoading chunk 2...\n""Loading chunk 1...\nLoading chunk 2...\n"
client render"""Loading chunk 1...\n""Loading chunk 1...\nLoading chunk 2...\n""Loading chunk 1...\nLoading chunk 2...\n"
Key Moments - 2 Insights
Why does the client see 'Loading chunk 1...' before the entire operation finishes?
Because the server streams data in chunks (see Step 3 and Step 5 in execution_table), the client renders each chunk as it arrives instead of waiting for all data.
What happens if the stream is not closed after sending chunks?
The client will keep waiting for more data and the page will not finish loading (see Step 6 where closing the stream signals completion).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the stream state after Step 5?
AClosed
BOpen
CNot started
DErrored
💡 Hint
Check the 'Stream State' column at Step 5 in the execution_table.
At which step does the client first render any content?
AStep 3
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Client Render' column in execution_table to see when content appears.
If the delay between chunks is increased, how does the execution_table change?
AData sent is reduced
BStream closes earlier
CStep 4's wait time increases, delaying Step 5 and client render
DClient renders all at once immediately
💡 Hint
Consider the wait in Step 4 and its effect on subsequent steps in execution_table.
Concept Snapshot
Streaming long operations in Next.js:
- Use ReadableStream in server components
- Enqueue data chunks progressively
- Close stream to signal end
- Client renders chunks as they arrive
- Enables faster perceived loading for long tasks
Full Transcript
This visual trace shows how Next.js streams long operations by sending data in chunks from the server to the client. The server creates a ReadableStream and enqueues parts of the response with delays. The client renders each chunk as it arrives, improving user experience by showing progress early. The stream closes when all data is sent, completing the page load.