A hydration error means the HTML your server sent doesn't match what React renders on its first pass in the browser. React then either throws the server HTML away and re-renders on the client, or it leaves mismatched attributes sitting in the DOM. To fix a hydration error in Next.js, you make that first client render identical to the server's. Silencing the warning doesn't count.

Nearly every case traces back to a short list of causes. One is a browser-only value like localStorage, Date or Math.random() read during render. Another is invalid nesting, such as a <div> inside a <p>. The third is an extension or CDN rewriting the page. Read the diff in the dev overlay before you wrap anything in ssr: false, and save suppressHydrationWarning for differences you expect and can't avoid.

What the error is telling you

Hydration is React taking over HTML the server already rendered. It assumes its first render in the browser produces exactly that markup. React 19 reports two different failures. "Hydration failed… this tree will be regenerated on the client" means a structural or text mismatch: React discards the server HTML and client-renders everything up to the nearest Suspense boundary. "A tree hydrated but some attributes… didn't match… This won't be patched up" is the quieter one. React keeps the existing DOM, and the wrong attribute stays where it is. On production React 18, the same failures show up minified as errors #418, #423 and #425, with no diff to go on.

So "the page looks fine" doesn't mean you're done. A failed hydration costs a full client re-render. If your nearest Suspense boundary sits high in the tree, that can be most of the page, and the server HTML you paid to generate gets thrown away. Attribute mismatches never get corrected at all. A stale aria-expanded, a class from the wrong theme, or an href pointing at the wrong URL stays in the DOM, where screen readers and crawlers still see it even if you don't.

Find the element that doesn't match

Don't change any code until you know which node differs. These checks run from cheapest to most effort, and most hydration errors are pinned down by step 2.

  1. Reload the page in an incognito or guest window with every extension off. If the error disappears, your code isn't the problem: an extension is editing the DOM before React hydrates it. Grammarly and ColorZilla are common offenders, and section 6 covers what to do about them. If the error survives, keep going.

  2. Read the +/- diff in the overlay or the console. React 19 logs a single error that shows the client render against the server HTML, and the Next.js 15 dev overlay displays the same diff. It usually names the exact element and the text or attribute that changed. This is the step people skip before wrapping random components in ssr: false, so don't.

  3. Open View Source, or disable JavaScript in DevTools and reload, and put it beside the Elements panel. Text or attributes that differ between the two mean your render logic branches between server and client. If the server markup has a <div> inside a <p> but the live DOM shows them as siblings, the browser's parser rearranged invalid nesting before React ever saw it.

  4. Run a production build locally with next build && next start, then check the deployed site. An error that appears only in the deployed site points outside your component code. The usual suspects are a CDN rewriting HTML, or a server running in UTC while your browser uses local time.

  5. Still nothing? Bisect. Comment out half the children of the failing page or layout and reload. If the error goes away, the culprit is in the half you removed, so restore it and split that half again. A few rounds will take you from hundreds of components to one.

Browser-only values like localStorage and theme

The server has no window, no localStorage, and no idea which theme the user picked. Any value that exists only in the browser has to wait until hydration is done. That makes a typeof window !== 'undefined' check inside render a trap, because it guarantees the server takes one branch and the client takes the other. You can check for the browser; just don't do it during render.

The classic case is a theme toggle that reads localStorage while rendering:

tsx
'use client';
import { useEffect, useState } from 'react';
// Broken: server renders "light", the client may render "dark"
export function ThemeToggleBroken() {
const theme =
typeof window !== 'undefined'
? localStorage.getItem('theme') ?? 'light'
: 'light';
return <button data-theme={theme}>{theme}</button>;
}
// Fixed: same default on both sides, real value after mount
export function ThemeToggle() {
const [theme, setTheme] = useState('light');
useEffect(() => {
setTheme(localStorage.getItem('theme') ?? 'light');
}, []);
return <button data-theme={theme}>{theme}</button>;
}

In the fixed version, the first client render matches the server exactly. The effect runs only after React has taken over the DOM.

If several components need this, wrap the pattern in a reusable hook:

tsx
'use client';
import { useEffect, useState } from 'react';
export function useMounted() {
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
return mounted;
}

Render a placeholder while mounted is false and the real UI once it flips.

The more idiomatic tool is useSyncExternalStore. Its getServerSnapshot argument supplies the value React uses on the server and during hydration, and after that React switches to the client snapshot. You don't need a hand-rolled state-and-effect pair. Its subscribe function can also listen for storage events, which keeps the value in sync across tabs.

The next-themes library goes another way. An inline script sets the theme class before React loads, so the <html> attributes deliberately differ from the server output. That's why its setup asks for:

tsx
<html lang="en" suppressHydrationWarning>

That flag silences only the <html> element's own attributes. Mismatches deeper in the tree still get reported.

Every deferred approach has a cost. The component renders twice, users can see a flash of the default theme, and anything rendered only after mount is missing from the server HTML, so crawlers never get it there. For a toggle icon that's acceptable. For real content it's bad. With themes, the cleaner fix is to store the preference in a cookie and read it on the server, so the very first HTML already carries the right class, with no flash and nothing to suppress.

Dates, times and random IDs

Date and time code is the most common reason a hydration error appears only after you deploy. On your laptop, the server and the browser share a timezone. In production, servers on Vercel or in containers usually run in UTC, while your visitors are in their own timezones and locales. The identical toLocaleString() call returns one string on the server and a different one in the browser.

Stop relying on defaults. Pin both the locale and the timezone so the server and the browser produce the same text:

tsx
const formatter = new Intl.DateTimeFormat('en-US', {
dateStyle: 'medium',
timeStyle: 'short',
timeZone: 'UTC',
});
export function PublishedAt({ iso }: { iso: string }) {
return <time dateTime={iso}>{formatter.format(new Date(iso))}</time>;
}
const fieldId = useId(); // same value on server and client

If readers need to see their local time, format it after mount and accept the same trade-offs as the deferred pattern above. Relative timestamps like "5 minutes ago" can drift between the server render and hydration, so put suppressHydrationWarning on the <time> element.

Never generate IDs with Math.random() or crypto.randomUUID() during render, since each side gets a new value. useId derives the ID from the component's position in the tree, so the server and the client always agree.

Invalid HTML nesting

The browser's HTML parser doesn't keep invalid markup the way you wrote it. When it hits a <div> inside an open <p>, it closes the paragraph first. <p><div>Hi</div></p> becomes a <p>, a sibling <div>, and a second, empty <p> created by the leftover closing tag. React then hydrates a DOM shaped differently from the tree it rendered and reports a structural mismatch. The Next.js docs flag <p> in <p>, <div> in <p>, <ul> in <p>, <a> in <a>, and <button> in <button>.

Server Components don't protect you here, because the rearranging happens in the browser after the server has finished. CMS rich text is the usual culprit: the body already contains <p> and <ul> tags, and it gets dropped into another <p> through dangerouslySetInnerHTML:

tsx
// Before: <p dangerouslySetInnerHTML={{ __html: post.body }} />
<div className="prose" dangerouslySetInnerHTML={{ __html: post.body }} />

Swap the wrapper tag. Suppressing the warning won't help, because the browser keeps rewriting the invalid markup on every load.

Extensions, CDNs and CSS-in-JS setup

If the diff points at an attribute or node you never wrote, something rewrote the HTML between your server and React. The question is whether you control it. Your users' extensions are out of your hands, so suppress those attributes or ignore them. Your CDN and your styling library are yours to configure, so fix them rather than hiding the warning. React 19 also tolerates tags injected into <head> and <body> better than React 18 did, so upgrading cuts down the noise (what changed with React 19 in Next.js 15).

  • Grammarly adds data-gr-ext-installed and data-new-gr-c-s-check-loaded to <body>. ColorZilla adds cz-shortcut-listen="true", and password managers and Dark Reader inject their own markup. suppressHydrationWarning on <body> is a legitimate fix here because it covers only that element's own attributes. Leaving them alone is fine too.
  • iOS Safari turns phone numbers into links. Add <meta name="format-detection" content="telephone=no"> to your layout's head.
  • Cloudflare's email obfuscation and Rocket Loader rewrite your HTML at the edge. Turn both off for the app.
  • In the App Router, styled-components and Emotion need their style registry set up. Without it, the styles and class names rendered on the server won't match what the client generates.

Choosing the right escape hatch

Each fix trades something away: crawler visibility, a flash on screen, or the chance of burying a real bug.

OptionIn server HTMLFlash or shiftCan hide bugsEffortBest for
useEffect / mounted check
No, only the fallback
Yes, content swaps in after mount
Low
Low
Small browser-only bits, like a saved preference
useSyncExternalStore
Only the server snapshot
Yes, unless the fallback matches
Low
Medium
Browser values you subscribe to, like localStorage or media queries
dynamic with ssr: false
No, the whole subtree is missing
Yes, placeholder until the JS loads
Low
Low
Heavy browser-only widgets like charts and maps
suppressHydrationWarning
Yes
No
High if misused
Minimal
Expected differences on one element: extension attributes, timestamps, theme attributes
Cookie / server-side data
Yes, with the correct value
No
Low
High
Themes and preferences you can know at request time

Start by removing the cause. A random ID gets useId, bad nesting gets different tags, and a date gets a fixed timezone. For browser APIs, reach for useSyncExternalStore or useEffect, and move to a cookie when the flash isn't acceptable. Use dynamic only when the entire widget can't run on the server. suppressHydrationWarning comes last: it won't help with a structural mismatch, and if you put it on a bug in your own code, the bug stays and you just stop hearing about it.

In the App Router, ssr: false throws if you call it from a Server Component. Put it in a small client wrapper and import that from your page:

tsx
// components/ChartClient.tsx
'use client';
import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('./Chart'), {
ssr: false,
// Reserve the chart's height so the page doesn't jump when it loads
loading: () => <div style={{ height: 320 }} aria-busy="true" />,
});
export default Chart;