Fixing React Hydration Mismatches in SSR Apps

Why React hydration mismatches happen with timestamps and locale strings, a small client-only wrapper that helps, and when SSR may not be needed.

· 2 min read · Erik Kubica

Opening browser developer tools to find a wall of red warnings is not a great start to the morning:

Warning: Text content did not match. Server: "10:00 AM" Client: "10:00:01 AM"

Often followed by its sibling:

Hydration failed because the initial UI does not match what was rendered on the server.

I have spent thirteen years going through frontend shifts, from raw jQuery DOM manipulation to Backbone, Angular, and React. In my experience, server-side rendering brings real benefits, and it also brings some bugs that are annoying to track down.

What Hydration Costs

The idea behind React SSR frameworks is appealing: you render the HTML on a server, send markup to the client for a fast First Contentful Paint, and then "hydrate" the DOM with JavaScript so users can click buttons and toggle tabs.

In practice, hydration has a cost.

First, you ship the complete HTML document. Then the user's browser downloads around 450 KB of JavaScript (in the project I had in mind) containing the same React components that created the HTML. Finally, the browser parses that bundle, runs the virtual DOM diff, and matches the nodes in memory against the server-rendered DOM.

Hydration means the browser downloads the code that produced the HTML, so it can check that the HTML matches.

If a single character deviates, such as a localized date format, a timezone difference between a London cloud server and a Tokyo user, or a window.innerWidth check, React can discard the server DOM and re-render on the client. In the case I measured that blocked the main thread for about 380 ms at full CPU.

The user can then see a visual flash, layout shift, and an unresponsive interface while React rebuilds the tree.

A Safe Client Boundary

When you need browser-specific state in an SSR application, it should not be rendered during the initial server pass. One approach is an explicit, deferred client boundary:

import { useState, useEffect, ReactNode } from "react";

interface ClientOnlyProps {
  children: ReactNode;
  fallback?: ReactNode;
}

export function ClientOnly({ children, fallback = null }: ClientOnlyProps) {
  const [hasMounted, setHasMounted] = useState(false);

  useEffect(() => {
    setHasMounted(true);
  }, []);

  if (!hasMounted) {
    return <>{fallback}</>;
  }

  return <>{children}</>;
}

By wrapping volatile elements, such as relative timestamps, local storage themes, or geolocation banners, inside a deferred mount boundary, the server renders a predictable placeholder. In my case the hydration pass then succeeded in about 10 ms without a mismatch warning, and the client rendered the real value once the window environment was available.

Do You Need SSR at All?

A question worth asking is whether the application needs SSR in the first place.

If you are building an authenticated client portal, a billing dashboard, or a SaaS tool behind a login screen, search engine crawlers will usually not see your pages. In that case SSR and hydration may add complexity without much benefit.

Sometimes a single-page application that renders once on the client and caches its assets well is the simpler choice. For public, content-heavy or SEO-sensitive pages, SSR is still the better fit, and a wrapper like the one above makes it manageable.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.