Sub topic 1
Why render on the server: CSR and SSR trade-offs
Prerequisites
This chapter assumes you can write React function components with props and state, and use useState and useEffect. You should know basic HTML and the browser DOM. You should also be able to run a small Node.js server such as Express and build JSX with a bundler. Later chapters use these tools. This one builds the mental model.
Two ways to produce a page
React has to turn components into DOM nodes. Where that happens changes what the user sees first.
With client-side rendering (CSR), the server sends a nearly empty HTML file and a JavaScript bundle. The browser downloads the bundle, runs it, and React builds the DOM. The page stays blank until that work finishes.
With server-side rendering (SSR), the server runs your components and sends finished HTML. The browser can display it before any JavaScript arrives. React then loads in the browser and attaches to that HTML so the page becomes interactive. That attaching step is called hydration. Later chapters cover it in detail.
What each approach costs
| Concern | CSR | SSR |
|---|---|---|
| First paint | After the bundle downloads and runs | From the first HTML response |
| Time to interactive | When the bundle runs | After the bundle runs and hydrates |
| Server load | Serves static files | Runs React on every request |
| Search engines and link previews | Must run JavaScript to see content | Receive the content in the HTML |
| Complexity | One environment | Code must run on both server and browser |
| Hosting | Any static host | A running Node.js process |
SSR improves how fast users see content. It does not make the page interactive sooner. Between the first paint and the end of hydration, buttons are visible but do nothing. A page that looks ready and isn't can frustrate users, so SSR is not automatically faster in every sense.
SSR also adds a constraint. Your components run in two places, so they must produce the same output in both. When they don't, you get a hydration mismatch. Most of this tutorial is about avoiding that and fixing it.
Choosing between them
Prefer SSR when:
- Search ranking or link previews matter, as with articles, product pages, and marketing pages.
- Users are on slow devices or networks, where a large bundle delays the first paint.
- The first meaningful content is the same for many visitors.
CSR is often enough when:
- The app sits behind a login and search engines never see it.
- The page is a dashboard where nearly everything depends on browser state.
- You can't run a server process.
Check your understanding
Exercise
A team builds an internal admin tool behind a login. Pages depend heavily on the current user's browser state. Which approach fits better, and what does SSR still not improve even for public pages?
Show answer
CSR fits better. No search engine needs to read the pages, and the heavy dependence on browser state makes server rendering harder with little benefit. For public pages, SSR improves first paint and crawlability, but it does not make the page interactive sooner. Interactivity still waits for the JavaScript bundle to load and hydrate.