← Back to Frontend, page 2

frontend-0025 · Medium mid

What is hydration, and what causes a hydration mismatch? How do you debug one?

Hydration attaches event handlers and state to HTML that the server already produced, reusing the existing nodes. A mismatch happens when the first client render produces different output than the server HTML, for example because of dates, random values or browser-only checks.

Full answer

With SSR or static generation, the browser first shows HTML that is not interactive: it has no click handlers and no state. When the JavaScript bundle loads, the framework runs the same components in the browser and walks the existing DOM next to the output. If they agree, it keeps the existing nodes and attaches the listeners. This is hydration. Compared with a full client render, it avoids throwing away and rebuilding the DOM, but the JavaScript still has to download and run, so a page can look ready and not respond for a moment.

A hydration mismatch is when the server HTML and the first client render are different. Typical causes:

  • Values that change between server and browser: Date.now(), new Date() with time zones, Math.random(), generated ids.
  • Code that reads browser-only data during render: window.innerWidth, localStorage, navigator.language.
  • Data that differs: the server rendered with one response and the client got another.
  • Invalid HTML nesting, such as a div inside a p. The parser repairs it, and the DOM no longer matches the markup that the framework expects.
  • Browser extensions or translators that change the DOM before hydration.

Frameworks react in different ways. React logs a warning or error in development and may discard the server HTML and render again on the client. The user can see a flicker, and the performance gain is lost.

Fixes:

  1. Make the first render depend only on data that is the same on both sides. Pass the server time or a seed as a prop.
  2. Move browser-only values into an effect, which runs only after hydration. Render a neutral value first.
  3. Use the framework's escape hatch for an unavoidable difference, such as suppressHydrationWarning in React for a timestamp text.
  4. Validate the HTML nesting.

The page below is a small model without a framework. The button in the HTML came from the server. Hydration checks that the markup agrees with what the client would render, then reuses the same node and adds the handler. If the numbers disagree, the check reports a mismatch.

html
<div id="app"><button id="like" type="button">Likes: 3</button></div>
<script>
  const render = (state) => `<button id="like" type="button">Likes: ${state.likes}</button>`;
  const state = { likes: 3 };
  const app = document.getElementById('app');
  const serverNode = app.firstElementChild;

  // Hydrate: compare, then reuse the existing node and attach the handler.
  window.mismatch = app.innerHTML !== render(state);
  if (!window.mismatch) {
    serverNode.addEventListener('click', () => {
      state.likes += 1;
      serverNode.textContent = `Likes: ${state.likes}`;
    });
  }
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The click handler updated the same button node twice, and changed server markup made the check report a mismatch. This is a small model without a framework. A real framework hydration error was not run.

Key points

  • Hydration reuses server HTML and attaches handlers and state.
  • A mismatch means the first client render differs from the server HTML.
  • Dates, random values and browser-only reads during render are the usual causes.
  • Move browser-only values into an effect, and pass shared data as props.
  • Mismatches can cost the performance gain and cause flicker.

Common mistakes

  • Reading window or localStorage directly in the render function.
  • Using the current time in the first render.
  • Hiding the warning instead of removing the cause.
  • Assuming a hydrated page is interactive as soon as it is visible.

Follow-ups

  • Why does invalid HTML nesting cause a mismatch?
  • What are islands and partial hydration?
  • How can you measure the time between a visible page and an interactive one?

Tips

  • List causes in two groups: different values and different data.
  • Give the effect fix, which is the one interviewers want.