Sub topic 1
Why render on the server: CSR and SSR trade-offs
Prerequisites
This chapter assumes you already know the following:
- Function components, props, and state in React.
- Hooks such as
useStateanduseEffect. - Basic HTML and the browser DOM.
- Basic Node.js and an HTTP server such as Express.
- JSX and a bundler setup.
No server rendering code is written here. The goal is to understand what each approach costs, so you can judge when the extra work of server rendering pays off. Later chapters build the implementation.
Two ways to produce the first screen
Every React page has to answer one question: who turns components into HTML, and when?
With client-side rendering (CSR), the server sends an almost empty HTML file and a JavaScript bundle. The browser downloads the bundle, runs React, and React builds the DOM. A typical CSR response looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shop</title>
</head>
<body>
<div id="root"></div>
<script src="/static/client.js"></script>
</body>
</html>Until client.js has loaded and run, the user sees a blank page. If the page needs data, the browser usually makes another request after React starts, so the real content arrives even later.
With server-side rendering (SSR), the server runs your React components and sends the resulting HTML. The browser can paint that HTML as soon as it arrives, before any JavaScript has run. The response contains real content:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shop</title>
</head>
<body>
<div id="root">
<h1>Shop</h1>
<ul>
<li>Keyboard</li>
<li>Mouse</li>
</ul>
</div>
<script src="/static/client.js"></script>
</body>
</html>The page is visible but not yet interactive. Buttons do nothing until React attaches to this HTML in the browser. That attaching step is called hydration, and most of this tutorial is about it.
The timeline difference
The diagram shows where time goes in each approach.
In CSR, content and interactivity arrive together, late. In SSR, content arrives early and interactivity follows. The gap between the two is the period in which the page looks ready but does not respond.
What SSR improves
- First paint. The browser shows meaningful content without waiting for the JavaScript bundle. On slow networks or low-end phones, this difference is large, because parsing and running a big bundle can take seconds.
- Data fetching latency. The server is usually close to your database or APIs. Fetching there avoids a browser-to-API round trip over a slow connection and avoids a chain of requests that starts only after the bundle runs.
- Crawlers and link previews. Many search engines run JavaScript, but not always reliably or quickly. Social link preview bots often do not run it at all. HTML that already contains the title, text, and meta tags works for every consumer.
- Resilience. If the bundle fails to load, the user still has readable content and working links.
What SSR costs
- Server load. With CSR, your server returns a static file, which a CDN can serve. With SSR, your server runs React for each uncached request. CPU use, memory, and latency under load become your concern.
- Time to first byte. A server that waits for all its data before sending anything delays the whole response. A slow query can make SSR feel slower than CSR. Streaming, covered in later chapters, exists to soften this.
- Two environments. Your components now run in Node.js and in the browser. Code that touches
window,document, orlocalStorageduring render fails on the server or behaves differently. - Hydration cost. The browser still downloads and runs all component code to hydrate. SSR improves when users see content, but it does not reduce the bundle size.
- Consistency requirement. The first client render must produce the same output as the server HTML. When it does not, you get hydration mismatches, which are the central failure mode of this tutorial.
- Operational complexity. You need a Node.js runtime in production, a build that produces both server and client bundles, and monitoring for server render errors.
Comparing the trade-offs
| Concern | Client-side rendering | Server-side rendering |
|---|---|---|
| First paint | After bundle download and execution | After the HTML response arrives |
| Interactivity | When the bundle runs | After hydration, which follows the bundle |
| Hosting | Static files on a CDN | A Node.js process or serverless function |
| Server cost per request | Very low | Runs React for each uncached request |
| Data fetching | Browser to API, often after the bundle runs | Server to API, before the HTML is sent |
| Crawlers and previews | Depend on running JavaScript | Receive real HTML |
| Code constraints | Browser APIs available everywhere | Render code must also work in Node.js |
| Typical failure | Blank page while loading | Hydration mismatch |
Deciding between them
Neither approach is better in general. Use these questions to decide.
- Is the content public and does it need to be found or previewed? Marketing pages, articles, and product listings benefit from SSR. A dashboard behind a login gains little, because crawlers never see it.
- Do users arrive on slow devices or networks? If so, a fast first paint matters, and SSR helps.
- Is the page mostly interactive tooling? An editor or admin panel needs the full bundle anyway. CSR keeps your infrastructure simple.
- Can your team run and monitor a Node.js server? If not, the operational cost may outweigh the benefit.
- Can the content be generated ahead of time? If it does not change per request, you can render HTML at build time and serve it statically. This gives the SSR benefits without per-request server cost. It is a different technique from request-time SSR, and this tutorial focuses on the latter.
Many applications mix approaches. Public pages use SSR while authenticated sections stay client-rendered.
Where this tutorial goes next
The rest of the tutorial follows the problems SSR creates. You will turn components into HTML, attach React to that HTML with hydrateRoot, and learn why the two renders must match. You will then stream HTML in pieces with Suspense, so a slow part of the page does not block the rest, and handle the errors that can occur along the way.
Exercise
A team builds an internal analytics dashboard. Every page requires login, and users open it on office desktops with fast networks. A developer proposes moving it to SSR to "make it faster." Give two reasons SSR may not be worth it here, and one change that would make SSR more attractive.
Show answer
First, crawlers and link previews never see pages behind a login, so SSR's search and sharing benefit does not apply. Second, fast devices and networks make the delay before first paint small, while SSR adds server cost, a Node.js runtime to operate, and the risk of hydration mismatches. The dashboard also needs the full bundle for interactivity regardless, so SSR does not reduce that cost. SSR would become more attractive if the team had users on slow networks or low-end devices, or if the dashboard exposed public, shareable pages that need link previews.
Exercise
A server-rendered page shows its article text almost instantly, but clicking the "Like" button does nothing for two seconds. Explain what is happening and which phase has not finished.
Show answer
The browser painted the HTML the server sent, so the text is visible. The JavaScript bundle is still downloading or running, and React has not yet hydrated the page, which means no event handlers are attached to the button. Hydration is the phase that has not finished. The delay is between first paint and interactivity.