Web Development

What is Hydration?

The browser-side step where a framework attaches event handlers and state to server-rendered HTML, turning static markup into an interactive app.

Definition

With server-side rendering (SSR) or static generation, the server sends complete HTML so users see content before any JavaScript runs. That HTML is inert: buttons do nothing until the framework's JavaScript loads. Hydration is the process in which the framework runs the same components in the browser, walks the existing DOM instead of recreating it, attaches event listeners, and rebuilds its internal state so later updates work normally. In React this is hydrateRoot; Vue, Svelte, Solid, and Angular have equivalents. Hydration assumes the client's first render produces exactly the same markup as the server's. If it does not, for example because a component reads window, the current time, Math.random(), or locale-specific formatting during render, the result is a hydration mismatch. React warns in development and may discard and re-render the affected subtree on the client, which costs performance and can attach handlers to the wrong elements. Text mismatches are not patched. The cost of hydration is that all the JavaScript for hydrated components must download, parse, and execute before the page responds to input, which hurts Interaction to Next Paint on slow devices. Newer approaches reduce that work: React's streaming SSR with Suspense hydrates sections independently, React Server Components never hydrate server-only components, Astro's islands architecture hydrates only components marked with client:* directives, and Qwik's resumability avoids replaying component code on load.

A real mismatch: the one fixed on this site

Until 14 Nov 2026 every page on TechLogHub logged Minified React error #418: the HTML the server rendered did not match what the browser rendered during hydration. The cause was a sidebar that picked two featured cards at random:

// Before: the initializer runs on the server, then again while hydrating
const [cards, setCards] = useState(() => shuffle(pool).slice(0, 2));

// After: the first render uses the order the server already chose
const [cards, setCards] = useState(() => pool.slice(0, 2));
useEffect(() => { /* rotate with shuffle() after mount */ }, []);

The server rendered one random pair, the browser computed another, and React discarded the server markup for that part of the tree. The fix was to make the first client render reproduce the server's output and move the randomness into an effect, which only runs in the browser after hydration. Finding it took one search: Math.random(), Date.now(), new Date() and locale formatting inside components that render on every page.

SSR, CSR and hydration compared

RenderingFirst paintJavaScript shippedInteractive when
Client-side rendering (CSR)Blank or a spinner until JavaScript runsThe whole appAfter the bundle loads and renders
SSR with full hydrationComplete HTMLThe whole appAfter hydration finishes
SSR with islands or partial hydrationComplete HTMLOnly the interactive widgetsPer island, often when it scrolls into view
React Server ComponentsComplete HTMLClient components onlyAfter those components hydrate

When to Use Hydration

Hydration matters whenever you use SSR or SSG with a client-side framework and want the page to be interactive. Accept full-page hydration for app-like pages where most of the UI is interactive anyway. For content-heavy pages (blogs, docs, marketing), prefer partial hydration or islands so only widgets such as search or a cart ship JavaScript. Keep render output deterministic: move browser-only reads into effects (useEffect, onMounted) so they run after hydration. If the page needs no interactivity, skip client JavaScript entirely. Watch Total Blocking Time and Interaction to Next Paint; a fast first paint followed by a long hydration delay creates a page that looks ready but ignores clicks.

Hydration in Practice

  • React client entry that hydrates server-rendered markup and reports recovered mismatches: import { hydrateRoot } from 'react-dom/client'; hydrateRoot(document.getElementById('root'), <App />, { onRecoverableError: (err) => reportError(err), });
  • Astro islands: the header is static HTML, the search box hydrates immediately, and the comments widget hydrates only when scrolled into view. <Header /> <Search client:load /> <Comments client:visible />
  • A mismatch fix: rendering new Date().toLocaleTimeString() directly differs between server and browser, so the component renders a placeholder and sets the time in useEffect after hydration.
  • A Next.js product page shows its Add to Cart button within 1 second, but clicks do nothing for another 3 seconds on a mid-range phone while a 600 KB bundle hydrates; splitting non-critical widgets into lazily hydrated islands closes the gap.

Common Mistakes

  • Reading window, localStorage, or matchMedia during render, which produces different output on the server. Move these reads into an effect that runs after hydration.
  • Using suppressHydrationWarning broadly to silence errors. It only applies one level deep and hides real bugs; fix the source of the difference.
  • Rendering timestamps, random IDs, or locale-formatted numbers inline. Use stable IDs (React's useId), pass server values as props, or format on the client after mount.
  • Assuming SSR makes a page fast. Fast HTML with a heavy hydration bundle still delays interactivity; measure INP and reduce what hydrates.
  • Producing invalid HTML nesting (a div inside a p). The browser repairs the DOM before hydration, so it no longer matches the framework's expected tree.

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.