Frontend / React Server Components

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.

Intermediate7 steps~7 min
Server Component — runs on the server; its code is never sent
Client Component — runs in the browser; holds state and clicks
Output — finished elements sent instead of code
Database — data only the server can reach
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 Every component ships
THE MATH
2 + 70 + 25 + 3 = 100 kB of JavaScript
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
VALUES SO FAR
all JS100 kBbefore
stays····server
shipped····browser
data····fetch
boundary····marker
payload····wire
hydrated····vs SSR
n····state
// AFTER

Learn next

Nothing builds on this one yet. See every frontend concept.

Spotted a mistake on this page?