# Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. (Next.js)

Appears in the browser console and the Next.js dev overlay as the page loads, with a diff of server versus client markup. In production React recovers by re-rendering on the client, so users see a flash and the console logs a minified error.

```
Error: Hydration failed because the server rendered text didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:

- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

https://react.dev/link/hydration-mismatch
  <time dateTime="2026-10-09T14:03:00Z">
+   10/9/2026, 9:03:00 AM
-   10/9/2026, 2:03:00 PM
```

## Quick fix

Render anything that differs between server and browser (Date.now(), Math.random(), window, localStorage, locale-formatted dates) only after mount inside useEffect, or format it with an explicit locale and timeZone on both sides. For a single unavoidable mismatch like a timestamp, add suppressHydrationWarning to that element.

1. Read the diff in the dev overlay: + lines are client values, - lines are server values.
2. Move window, localStorage and Date.now() reads into useEffect with useState.
3. Format dates with a fixed locale and timeZone, or format them only on the client.
4. Fix invalid nesting such as a <div> inside a <p> or an <a> inside an <a>.
5. Reload in a private window with extensions off to rule them out.
6. Use suppressHydrationWarning only on the one element whose text must differ.

```ts
'use client';
import { useEffect, useState } from 'react';

export function LocalTime({ iso }: { iso: string }) {
  const [text, setText] = useState<string | null>(null);

  useEffect(() => {
    // Runs only in the browser, after hydration, so the first render matches the server
    setText(new Date(iso).toLocaleString());
  }, [iso]);

  return <time dateTime={iso}>{text ?? iso}</time>;
}
```

## Why it happens

### Browser-only branches during render

typeof window !== 'undefined' checks, localStorage reads and matchMedia calls give one result on the server and another in the browser. The first client render must match the server HTML, so these reads belong in an effect.

### Time and randomness

Date.now(), new Date() and Math.random() return different values in the two renders. A relative label like '3 minutes ago' can also flip between server and client when the minute rolls over.

### Locale and time zone formatting

toLocaleString() with no arguments uses the server's locale and time zone (often en-US and UTC in containers) on the server, then the visitor's settings in the browser. Pass a fixed locale and timeZone, or format on the client.

### Invalid HTML nesting

The browser's HTML parser repairs invalid markup before React hydrates: a <div> inside a <p> closes the paragraph early and nested <a> tags get split apart. React then finds a different DOM tree than the one it rendered.

### Browser extensions editing the page

Password managers, translators and grammar tools inject attributes or nodes into <html>, <body> or form fields before React loads. These mismatches only appear for visitors with that extension, which is why they are hard to reproduce.

### Data that changed between the two renders

The server renders with one API response and the client fetches again during its first render and gets newer data. Pass the server snapshot down as props instead of refetching before hydration finishes.

## Other fixes

- Load a component that cannot render on the server with next/dynamic and ssr: false from inside a client component.
- Add suppressHydrationWarning to <html> and <body> in the root layout when theme scripts or extensions change their attributes.
- Read browser stores through useSyncExternalStore with a getServerSnapshot so server and first client render agree.
- Generate element ids with useId instead of a random id helper.

## Same error, other wording

- `Error: Hydration failed because the initial UI does not match what was rendered on the server.`
- `Warning: Text content did not match. Server: "x" Client: "y"`
- `Minified React error #418`
- `A tree hydrated but some attributes of the server rendered HTML didn't match the client properties. This won't be patched up.`

## Sources

- [Next.js: Text content does not match server-rendered HTML](https://nextjs.org/docs/messages/react-hydration-error)
- [React: hydrateRoot](https://react.dev/reference/react-dom/client/hydrateRoot)

---

To see this error the next time it happens in production, with the stack trace and the steps before it: Runheed records it and serves it to your coding agent over MCP. Setup instructions an agent can follow: https://runheed.com/SETUP.txt

Human-readable version: https://runheed.com/errors/hydration-mismatch
