# Rendering Patterns

> Understanding the difference between Client-Side Rendering (CSR), Server-Side Rendering (SSR), and Static Site Generation (SSG) in modern web development.

Subject: React | Lesson 45 of 48 | 7 min read
Interactive lesson: https://codewithanimation.in/react/rendering-patterns
Previous: [Testing React](https://codewithanimation.in/react/testing-react.md) | Next: [React Server Components (RSCs)](https://codewithanimation.in/react/react-server-components.md)

## Where does the HTML come from?

In standard React (like Create React App or Vite), the server sends an empty HTML file: `<div id="root"></div>`. The browser downloads a massive JavaScript bundle, executes it, and finally paints the UI. This is **Client-Side Rendering (CSR)**.

**Server-Side Rendering (SSR)** shifts this work to the server. When a user requests a page, the server runs the React code, fetches the database, generates the full HTML, and sends it to the browser. The user sees the UI instantly while the JavaScript loads in the background.

## SEO and Perceived Performance

CSR is terrible for SEO because web crawlers often see a blank page. It's also bad for users on slow networks, as they stare at a white screen or spinner while waiting for JavaScript to download and APIs to resolve.

SSR solves both. Search engines instantly index the fully rendered HTML. Users perceive the site as lightning-fast because the UI paints immediately, even if it takes a second for the buttons to become interactive (a process called *hydration*).

## The Hydration Process

When using SSR, the server sends static HTML. But HTML alone isn't interactive (you can't `onClick` a static button).

Once the browser paints the HTML, it then downloads the React JavaScript bundle and attaches event listeners to the existing DOM nodes. This process of 'waking up' the static HTML is called **Hydration**.

## Common mistake: Server Costs and Time To First Byte

SSR isn't a silver bullet. Because the server has to build the page dynamically on *every single request*, it requires more expensive server infrastructure.

If your database is slow, SSR will cause a slow 'Time To First Byte' (TTFB). The user stares at a blank browser tab waiting for the server to finish before they see anything at all.

## Tip: Static Site Generation (SSG)

For pages that don't change often (like a blog post or a marketing page), use **Static Site Generation (SSG)**.

With SSG, you render the HTML on the server exactly *once* during the build step (when you run `npm run build`), and then cache it on a CDN. This gives you the SEO benefits of SSR but with zero server cost and instant TTFB.

## Try it: The Network Race

Watch the animated difference between Client-Side Rendering (CSR) and Server-Side Rendering (SSR). Notice where the 'heavy lifting' (fetching data and painting UI) happens in each paradigm.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/rendering-patterns

## Check yourself

### Question 1

What is the primary difference between Client-Side Rendering (CSR) and Server-Side Rendering (SSR)?

- A. CSR writes code in JavaScript, while SSR writes code in PHP.
- B. In CSR, the browser builds the HTML using JavaScript. In SSR, the server builds the HTML and sends it ready-to-view.
- C. CSR is always faster than SSR because it uses the user's local CPU.

Correct answer: B

CSR sends an empty shell and makes the browser do the heavy lifting. SSR does the work on the server and sends a fully populated HTML document.

### Question 2

What is 'Hydration' in the context of SSR?

- A. The process of optimizing images before sending them to the client.
- B. The process of the browser attaching React event listeners to the static HTML sent by the server to make it interactive.
- C. The process of clearing the browser cache.

Correct answer: B

The server sends dead HTML. React 'hydrates' it by walking the DOM and attaching `onClick` and `onChange` handlers so the page actually functions as a React app.

### Question 3

When should you prefer Static Site Generation (SSG) over Server-Side Rendering (SSR)?

- A. When building a highly personalized dashboard (like a bank account).
- B. When the page content is public and updates infrequently (like a blog or documentation site).
- C. When the page relies heavily on WebSockets for real-time chat.

Correct answer: B

SSG is perfect for public, static content because the HTML is pre-built once and cached globally on a CDN, making it incredibly fast and cheap to serve.

## Remember this

- CSR: Browser gets an empty div, downloads JS, fetches data, builds UI. Slow initial load, bad SEO.
- SSR: Server fetches data, builds HTML, sends to browser. Fast initial load, great SEO, requires server computing.
- Hydration: The process of attaching React event listeners to server-rendered HTML.
- SSG: HTML is built once at compile time. Fastest and cheapest, but cannot handle highly dynamic user-specific data.
- Modern frameworks (like Next.js) allow you to mix CSR, SSR, and SSG on a per-page basis.
