0% completed
Streaming and Server Components
On This Page
One Slow Part Holds Back the Page
Suspense Boundaries
Server Components
The Boundary Between Server and Client
Partial Prerendering
Using This in an Interview
Key Takeaways
Practice Questions
Streaming HTML means the server sends a page in pieces, each as soon as it is ready, inside one response, instead of waiting until the whole page is ready. The browser draws each piece as it arrives.
A server component is a component that runs only on the server, so its code is never sent to the browser. What the browser receives is the component's result, not the component.
The two ideas solve different problems on the same page. Streaming moves the first content earlier when one part of the page is slow. Server components make the JavaScript smaller when much of the page never changes after it is drawn. This page explains how each one works, using Basket's product page, and where the line between server code and browser code sits.
One Slow Part Holds Back the Page
Basket's product page needs three kinds of data. Suppose the product details take 60 ms to fetch, recommendations take 400 ms, and reviews take 900 ms, because the reviews service is slow.
With server rendering that sends one complete response, the server cannot send the first byte until it has everything. Even with all three requests started at once, the first byte waits 900 ms for the reviews. Started one after another, the requests add up to 1,360 ms.
With streaming, the server sends the page header, the product details and two gray placeholders after 60 ms. It sends the recommendations at 400 ms and the reviews at 900 ms, in the same response.
Look at the first piece: at 60 ms the shopper has the product name, the photo and the price, and nothing waits for the reviews. Notice that the single-response row sends nothing for 900 ms, although most of its content was ready at 60 ms.
The mechanism is plain HTTP. HTTP/1.1 sends a body of unknown length in chunks, and HTTP/2 and HTTP/3 send it as a series of data frames. The browser's parser already builds the DOM from bytes as they arrive, as described in How a Browser Turns HTML into Pixels, so it can draw the first piece before the last one exists.
Suspense Boundaries
React marks the parts that may arrive later with a Suspense boundary: a wrapper that names the part that may wait and the fallback to show until it is ready.
<ProductDetails product={product} /> <Suspense fallback={<ReviewsSkeleton />}> <Reviews productId={product.id} /> </Suspense>
The server sends the fallback in the first piece. When the reviews are ready, the server appends their HTML to the same stream, after everything sent so far. That HTML arrives inside a hidden element, and a short inline script moves it into the place the fallback held. The pieces can therefore arrive in any order, and the page can still be read top to bottom.
Streaming has two limits. First, the HTTP status code and headers go out with the first piece, so a page cannot turn into a 404 or a redirect after streaming starts. The product page must therefore load the product itself before it sends anything, and only the optional parts go inside boundaries. Second, every boundary adds a placeholder that is later replaced, so the placeholder must have the same size as the content, or the page jumps when the content arrives.
Server Components
A classic server-rendered React page sends HTML, and then it sends the code of every component on the page so the browser can hydrate it. That includes components that never respond to anything, such as the product description.
Server components remove that code. A server component runs on the server, at request time or at build time, and it can read data directly, for example by calling an internal service with a secret key that must never reach the browser. Its output is a serialized description of the rendered tree, often called the RSC payload, which the browser merges into the page. The component's own code, and any library it imports, is never downloaded.
Suppose Basket formats product descriptions with a 60 KB Markdown library. As code in a browser component, that is 60 KB to download, about 0.3 seconds at 200 KB per second, plus the time to run it. As a server component, it costs the browser nothing.
What a server component gives up is everything that happens after the page is drawn. It has no state, no effects, no event handlers, and no access to browser APIs such as window.
The Boundary Between Server and Client
A client component is a component whose code is sent to the browser so it can hold state and respond to input. In React, a file that starts with 'use client' marks the boundary: that file and everything it imports become client code. Client components are still rendered to HTML on the server for the first load, and then hydrated.
'use client' export function AddToCartButton({ productId, price }: { productId: string; price: number }) { // state, a click handler, a request to the cart API }
Look at which boxes ship code: only the two that respond to the shopper. Notice that the arrows cross the boundary in one direction, from server to client, and carry data, not functions.
Everything that crosses the boundary travels as data, so it must be something React can serialize.
| Can cross from server to client | Cannot cross |
|---|---|
Strings, numbers, booleans, null | Ordinary functions, such as an onClick handler |
| Arrays and plain objects of those | Class instances, such as a database row object |
Date, Map, Set, promises | Symbols that are not registered globally |
JSX, such as a server component passed as children |
Serializable does not mean safe. A string that holds a secret key crosses the boundary without any error, and then it is in the page that every visitor downloads. Pass a client component only the fields it shows.
One function is an exception. A server function, marked with 'use server', can be passed to a client component, which then calls it as a request to the server. A client component also cannot import a server component, because its imports become browser code. It can still display one that a server component passes to it as children.
Partial Prerendering
Partial prerendering combines the static generation from Static Generation and Incremental Regeneration with streaming. The page's shell, the parts that are the same for everyone, is built ahead of time and served at once from the CDN. The personal or slow parts are then streamed into the places left for them, in the same response. Next.js introduced the technique under that name.
| Classic server rendering | Streaming server rendering | Server components | |
|---|---|---|---|
| First byte | After the slowest data | After the first piece is ready | Depends on whether the page streams |
| What arrives | One HTML document | HTML in pieces | HTML plus a serialized tree |
| Code sent to the browser | Every component | Every component | Client components only |
| Main gain | Content in the HTML | Earlier first content | Less JavaScript to download and hydrate |
Using This in an Interview
Separate the two choices: "I stream the product page so reviews never hold back the price, and I keep everything except the cart button and the gallery as server components, so their code never ships."
Two follow-up questions are likely:
- "Why can't the page return a 404 after streaming has started?"
- "Why can't you pass an
onClickfunction from a server component to a client component?"
Key Takeaways
- Streaming sends a server-rendered page in pieces inside one response, so a slow part no longer delays the first byte.
- A Suspense boundary names a part that may wait and its fallback. The late HTML arrives later in the same stream and replaces the fallback.
- The status code and headers leave with the first piece, so anything that decides them must be ready before streaming starts.
- A server component runs only on the server and sends its result, never its code, so its libraries cost the browser nothing. It cannot hold state or handle input.
- A
'use client'file and its imports become browser code. Props that cross the boundary must be serializable data, and ordinary functions cannot cross.
Client components still arrive as HTML that looks ready before its code runs. The next lesson, Hydration and Its Cost, measures that gap and the ways to shorten it.
Practice Questions
Try each question first, then open the answer.
1. A page needs data that takes 50 ms, 300 ms and 700 ms, fetched at the same time. When can the first byte leave the server with one complete response, and with streaming?
<details> <summary>Show answer</summary>At about 700 ms with one complete response, and at about 50 ms with streaming. A complete response waits for the slowest data. Streaming sends the first piece when its own data is ready and puts the 300 ms and 700 ms parts inside Suspense boundaries, whose HTML arrives later.
</details>2. The product page streams, and the product turns out not to exist after the first piece has gone. What can the page do, and how should it be designed instead?
<details> <summary>Show answer</summary>It can only show a "not found" message inside a page that already returned status 200. The status code left with the first piece. The design should load the product before streaming begins, so a missing product returns a real 404, and put only the optional parts, such as reviews, inside boundaries.
</details>3. A server component passes onAdd={() => addToCart(id)} to a client component. What happens, and what are the two ways to fix it?
React rejects it, because an ordinary function cannot be serialized across the boundary. One fix is to pass only data, such as the product id, and write the click handler inside the client component. The other is to pass a server function marked with 'use server', which the client calls as a request.
4. Basket's product description uses a 60 KB Markdown library. Should the description be a server or a client component, and what does the choice save?
<details> <summary>Show answer</summary>A server component, which saves the 60 KB download and the time to run it. The description does not respond to input, so it needs no browser code. On the slow profile, 60 KB is about 0.3 seconds of download that the shopper no longer waits for.
</details>5. Why must a Suspense fallback have about the same size as the content that replaces it?
<details> <summary>Show answer</summary>Otherwise the page moves when the late content arrives. If a 40 pixel placeholder becomes a 600 pixel reviews section, everything below it jumps down. The shopper loses their place and may tap the wrong thing.
</details>Reading Progress
0%
On This Page
One Slow Part Holds Back the Page
Suspense Boundaries
Server Components
The Boundary Between Server and Client
Partial Prerendering
Using This in an Interview
Key Takeaways
Practice Questions