← All posts

· 7 min read

SSR + React Hydration Without the Mismatch Warnings

The hydration contract

When React hydrates a server-rendered HTML document, it walks the virtual DOM it would have produced and compares it — node by node — against what the server actually sent. Any mismatch causes React to silently discard the server HTML and re-render the entire subtree on the client. The page does not break, but the whole point of SSR — shipping a fully-rendered document that the browser can display before JavaScript runs — is lost.

In this project I ran into three categories of mismatch. Each had a different root cause, and each required a different fix.

Cause 1: State initialised differently on server and client

The most common pattern I hit was reading something from the browser environment inside a useState initializer:

// DON'T: this diverges server from client
const [theme, setTheme] = useState(
  typeof window !== 'undefined' ? localStorage.getItem('theme') : null
);

On the server typeof window is always 'undefined', so the initial state is null. On the client it immediately reads localStorage and may get 'dark'. React sees two different renders and warns.

The fix is a two-phase approach: initialize with the server-safe value on both sides, then read the client-only source after hydration in a useEffect:

const [theme, setTheme] = useState(null); // typed as string | null

useEffect(() => {
  setTheme(localStorage.getItem('theme'));
}, []);

This produces matching HTML on both sides. The useEffect fires after hydration, so the user may briefly see the default state — but that is a UX tradeoff, not a hydration error.

Cause 2: Dynamic class injection by the CSS-in-JS runtime

MUI uses Emotion for styling. Emotion generates class names from a counter that starts at 0 on every render. If the server generates class css-1 for a component, and the client regenerates the same component tree, it should also produce css-1 — as long as the render order is identical.

The project injects an Emotion cache server-side (using @emotion/server), serializes the generated styles into the HTML <head>, and passes the same cache key to the client. Sharing the key prevents the client from regenerating class names from scratch, which is what causes the mismatch.

  • Server: create cache → render → extract critical CSS → embed in HTML
  • Client: hydrate using the same cache key → Emotion recognises already-injected styles and reuses them

Cause 3: Locale-dependent content rendered before the locale is resolved

The site is bilingual. The server detects the locale from the request URL. If a client component renders a locale-specific string during initial render before the locale context is initialised, the server and client trees will diverge.

The solution is to serialize the server-detected locale into the HTML (as a global variable on window, or as a data attribute on <html>) and read it synchronously during initialisation on the client. Both sides then start from the same locale value.

The general rule

Every piece of state that flows into the initial render must be either:

  1. Deterministic from the request (and passed from server to client via the HTML), or
  2. Intentionally deferred past hydration via useEffect

Branching on typeof window !== 'undefined' inside render functions is the single biggest source of SSR/client mismatches. Avoid it everywhere in the render path.