Server vs Client Components
IntermediateWhat runs on the server and what runs in the browser: a Server Component that fetches data, a "use client" button, the HTML and RSC payload, and hydration.Builds on What happens when state changes, What happens when you type a URL
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What are Server and Client Components in Next.js?
In the App Router, every component is a Server Component unless you say otherwise. A Server
Component runs only on the server: it can be async, read a database or a file, use secrets, and
its code is never sent to the browser. What it sends is its result.
A Client Component is one that needs the browser: state (useState), effects, event handlers
like onClick, or browser APIs. You mark its file with 'use client' at the top. Client Components
are also rendered to HTML on the server for the first load, so "client" does not mean "only in the
browser"; it means its code is sent to the browser too, so it can run there.
What runs where when a page loads?
- On the server, React renders the Server Components and puts their result in the RSC payload: the rendered output, a placeholder for each Client Component, the props passed to it, and a reference to its JavaScript file. Together with the Client Components, that payload is used to prerender HTML.
- In the browser, the HTML shows the page right away, before any JavaScript runs.
- The browser downloads the JavaScript for Client Components only and hydrates them: React runs them again, matches the HTML already on screen, and attaches their event handlers.
- After that, interactions inside a Client Component, like clicking the button, run in the browser. No request to the server is needed unless your code makes one.
'use client' marks a . Everything that file imports, and every component it renders
directly, becomes part of the client bundle. Components above the boundary stay on the server.
What mistakes do people make with Server and Client Components?
Mini example
Comments stays a Server Component even though it is shown inside the interactive Collapsible:
the page renders it on the server and passes the result as children.
// app/page.tsx: a Server Componentexport default function Page() { return ( <Collapsible> <Comments /> </Collapsible> );}// app/collapsible.tsx'use client'; export function Collapsible({ children }: { children: React.ReactNode }) { const [open, setOpen] = useState(false); return ( <section> <button onClick={() => setOpen(!open)}>Comments</button> {open && children} </section> );}Quick check
Which of these can a Server Component pass as a prop to a Client Component?
Further reading
All steps as text
- Step 1:
You open the page. The browser sends a request for / to the Next.js server.
- Step 2:
On the server, React runs Page, a Server Component. It is an async function, so it can wait for data directly.
Lines 3–4
- Step 3:
getPostreads the post straight from the database. The browser never talks to the database, and never receives this code.Line 4
- Step 4:
Page returns an article with a LikeButton in it. LikeButton is a Client Component: it is rendered to HTML here too, but its code will also go to the browser.
Lines 5–10
- Step 5:
The response carries the HTML and the RSC payload: Page’s result, a placeholder for LikeButton, its props and where its code is.
Lines 6–8
- Step 6:
The browser shows the HTML at once: the title and
♥ 3are on screen, but the button doesn’t respond yet. - Step 7:
Then it downloads JavaScript, but only for Client Components: LikeButton’s code, plus React. Page’s code stays on the server.
Lines 13–15
- Step 8:
Hydration: React runs LikeButton in the browser with
likes = 3from the payload, and attaches its click handler to the button already on screen.Lines 16–18
- Step 9:
You click.
setCountruns in the browser and the button shows♥ 4, with no request to the server.Line 19
- Step 10:
Server Components run only on the server and send their result. Client Components also send their code, so they can run in the browser and respond to you.