React Server Components
One product page, four components: see which of them run only on the server, what is sent to the browser instead of their code, and where 'use client' draws the line.
In 30 seconds
A React Server Component runs only on the server. The browser receives what it rendered, never its code, so the libraries it uses add nothing to the download.
Because it runs on the server it can read a database directly. It cannot hold state or handle clicks: for that, a file marked 'use client' starts a Client Component, whose code is sent as usual.
This differs from classic server-side rendering, which sends HTML first but still ships every component and runs it again in the browser.
React Server Components, step by step
Sizes are invented round numbers, db is a made-up helper and the payload is simplified. React itself is still downloaded. Server Components need a framework that supports them, such as Next.js.
- Step 1: Every component ships. A product page built from four ordinary React components. To show it, the browser must download and run the code of all four, including the Markdown parser and the date library that two of them use. Only the Add button ever responds to a click.
- Step 2: Server components stay home. A Server Component runs only on the server (or at build time) and its code is never sent. Page, ProductInfo and Reviews display data but never react to the user, so they can stay behind together with their libraries. Only AddToCart has to run in the browser.
- Step 3: Fetch data where it lives. A Server Component can be an async function. It waits for the database directly, with no API endpoint in between and no data request from the browser. Database passwords and API keys are used on the server and never appear in code the user can read.
- Step 4: The client boundary. In a framework that supports them, components are Server Components by default. A file that starts with 'use client' marks the boundary: it and everything it imports become Client Components, which can hold state and handle clicks. Props that cross the boundary must be data that can be written out as text, such as id = 7.
- Step 5: What crosses the wire. The server runs its components and sends their result, not their code: a description of the finished elements, with a placeholder where each Client Component goes. The placeholder names the file to load and carries the props. React in the browser fills it in with the real AddToCart.
- Step 6: Not the same as SSR. Classic server-side rendering also produces HTML on the server, but then ships every component and runs it again in the browser to attach event handlers (hydration). Server Components skip that second run entirely. In practice the two are combined: Client Components are still rendered to HTML for the first load, then hydrated.
- Step 7: Refresh, keep the state. The user has clicked Add twice. A new review is posted and the page asks the server for a refresh. The server re-runs its components and sends a fresh payload. React merges it into the page on screen instead of replacing it, so the review count changes while the button keeps its state. A full page reload would reset it to 0.
Try it yourself
Learn this first
Spotted a mistake on this page?