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
- The server runs your components with the request's data and sends finished HTML.
- The browser displays it straight away: good for perceived speed and for crawlers that read HTML.
- 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
windowduring 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.
What should you learn before Server-side rendering and hydration?
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
- Advanced React Interview Questions2 questions on Server-side rendering and hydration
- Intermediate React Interview Questions1 question on Server-side rendering and hydration