Next.js “Hydration failed”: Causes and Working Fixes
Hydration fails when the first render in the browser differs from the server’s HTML. How to find the mismatch, with the fix we use on our own contact form.
December 3, 2026 · 4 min read
"Hydration failed" means the HTML from your server does not match what React rendered first in the browser. Find the value that differs between the two, then either make it the same or render it only after hydration.
The message usually looks like this:
Hydration failed because the server rendered HTML didn't match the client.
As a result this tree will be regenerated on the client.
Find the mismatch first
- Run
next devand open the error overlay. Recent versions show the server and client output side by side. - Open the page in a private window with extensions turned off. If the error disappears, an extension changed the page.
- Compare View source (server HTML) with the element in DevTools.
- If you still cannot see it, remove half the page's client components, then half of what is left.
Cause 1: a value that differs between server and browser
Time zones, the current time, random numbers, window and localStorage are the usual suspects.
'use client';
// Wrong: the server and the visitor can be in different time zones
export function Posted({ date }: { date: string }) {
return <time>{new Date(date).toLocaleString()}</time>;
}
If the value can be the same for everyone, fix the locale and time zone:
export function Posted({ date }: { date: string }) {
const text = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
timeStyle: 'short',
timeZone: 'UTC',
}).format(new Date(date));
return <time dateTime={date}>{text} UTC</time>;
}
Cause 2: content that can only exist in the browser
Our own contact form guesses your country code from your time zone. The server cannot know that, so the form renders after hydration. This is the component we use:
'use client';
import { useSyncExternalStore } from 'react';
const subscribe = () => () => {};
export function useHydrated() {
return useSyncExternalStore(subscribe, () => true, () => false);
}
export default function ClientOnly({ children }: { children: React.ReactNode }) {
return useHydrated() ? children : null;
}
useSyncExternalStore takes a separate value for the server. React uses false on the server and during hydration, then switches to true. The first render always matches, and no effect has to set state.
Two warnings:
- Wrap the smallest part you can. Content inside
ClientOnlyis missing from the server HTML, so do not wrap text that search engines need. - Reserve the space with a fallback of the same height, or the page will jump.
Cause 3: invalid HTML nesting
Browsers repair invalid HTML before React sees it. A <div> inside a <p>, or a link inside a link, changes the tree.
// Wrong
<p>Read more <div>here</div></p>
// Right
<p>Read more <span>here</span></p>
This often comes from a component that renders a div placed inside a paragraph of CMS or Markdown text.
Cause 4: checking for window during render
// Wrong: the server renders one thing, the browser another
const isMobile = typeof window !== 'undefined' && window.innerWidth < 768;
return isMobile ? <MobileMenu /> : <DesktopMenu />;
For layout, use CSS media queries and let one HTML output serve both. For behaviour, use the useHydrated hook above.
Cause 5: theme scripts and browser extensions
A dark-mode script that adds a class to <html> before React loads creates a difference on purpose. Add suppressHydrationWarning to that one element:
<html lang="en" suppressHydrationWarning>
It works one level deep and only hides the warning for that element. Do not use it to silence errors you have not understood.
Cause 6: stale or rewritten HTML
If the error appears only in production, check whether a CDN is minifying or rewriting HTML, or serving an old page with new JavaScript. Compare the response from your host with the one visitors get.
Quick reference
| What you see | Try first |
|---|---|
| Dates or prices differ | Fixed locale and time zone |
| Error only on your computer | Private window, no extensions |
| Error only in production | CDN rewriting or old cache |
| Different layout on mobile | CSS instead of window.innerWidth |
| Error after adding a theme switch | suppressHydrationWarning on <html> only |
| Error around rich text | Invalid nesting |
Before you release
Build and run production locally with next build and next start. Then test with another time zone (DevTools → Sensors), a fresh private window and a slow connection.
Still broken?
Send us the error and the page where it happens. Our bug fixing service starts from $49 with a written diagnosis first. If errors keep coming back, a code audit or monthly maintenance is better value than fixing them one at a time. For speed problems, see our Core Web Vitals checklist.
More in Website speed, security and care