learn deep. ship safe.Practice testsFollow ByteMunSubscribe

Concept

Server-side rendering and hydration

Rendering HTML on the server, then attaching interactivity in the browser.

Server-side rendering and hydration explained

Server-side rendering (SSR) builds a page's HTML on the server for each request, so the browser shows real content immediately instead of an empty shell waiting for JavaScript. Hydration then attaches the React app to that HTML in the browser, adding event handlers and state so the page becomes interactive.

How SSR and hydration work

  1. The server runs your components with the request's data and sends finished HTML.
  2. The browser displays it straight away: good for perceived speed and for crawlers that read HTML.
  3. The JavaScript bundle loads and React hydrates: it renders the same tree in the browser and connects it to the existing HTML instead of recreating it.

SSR, static generation and client rendering

  • Client-side rendering: the server sends a shell; JavaScript fetches data and builds the page. Slower first content, weaker for search engines.
  • Static generation: HTML built at build time (or cached and revalidated). Fastest, for pages that are the same for everyone.
  • SSR: HTML per request. Best for personalised or frequently changing pages.
  • Streaming SSR sends the page in chunks, so fast parts appear while slow data is still loading.

Hydration mismatches

Hydration expects the browser's first render to match the server's HTML. If it differs, React warns and may re-render. Common causes are dates and times, Math.random(), reading window or localStorage during render, and invalid HTML nesting. Move browser-only values into an effect, or render them only on the client.

Common mistakes

  • Shipping a huge bundle: the HTML appears fast but the page is not interactive until hydration finishes.
  • Accessing window during render, which crashes on the server.
  • Fetching the same data on the server and again in the browser.

What interviewers ask about SSR

  • "What is hydration?" Attaching React to server-rendered HTML to make it interactive.
  • "SSR or static generation?" Static when content is shared and changes rarely; SSR when it is personal or live.
  • "How does SSR help SEO?" Crawlers get complete content in the HTML.
Concept map
Learn firstRendering
Server-side rendering and hydration

What should you learn before Server-side rendering and hydration?

  1. PropsInputs passed from a parent component to a child.
  2. StateData that changes over time and makes the UI re-render, and where to keep it.
  3. RenderingWhen and why components render, and what triggers a re-render.

What to learn after Server-side rendering and hydration

  • React Server ComponentsComponents that run only on the server and send rendered output, not their code, to the browser.

Practise Server-side rendering and hydration

All topics