Next.js "Hydration failed because the initial UI does not match": 6 causes, the fix, and the one that leaks a key
Your Next.js app shows this in the console, usually after you deploy or add a new component: Error: Hydration failed because the initial UI does not match what was rendered on the server. or "Text content does n
Your Next.js app shows this in the console, usually after you deploy or add a new component:
Error: Hydration failed because the initial UI does not match what was rendered on the server.
or "Text content does not match server-rendered HTML". The page may flash, break, or work after a refresh. Here is what it means, and the one "fix" that creates a security problem.
What it means
Next.js renders your page to HTML on the server, then React "hydrates" it in the browser: it attaches events and expects the same markup. If the first browser render produces different HTML than the server sent, React reports a hydration mismatch.
The 6 usual causes
-
Browser-only values in render.
window,localStorage,navigatorordocumentused while rendering give different results on the server and client. -
Time and randomness.
new Date(),Date.now()orMath.random()in the render output differ between the two renders. -
Invalid HTML nesting. A
<div>inside a<p>, or a<p>inside a<p>. The browser repairs it and the DOM no longer matches. - Browser extensions that inject nodes into the page (translation, dark mode, password managers).
- Different data on server and client. The server fetched one value, the client state starts with another.
- Locale and timezone formatting. Dates and numbers formatted differently on the server and the user's device.
The fix, in order
- Read the diff React prints. It names the element that differs.
- Move browser-only reads into
useEffectand render a stable placeholder first. - Format dates in one fixed locale and timezone, or only on the client after mount.
- Fix invalid nesting.
- For truly client-only components, load them with
dynamic(() => import(...), { ssr: false }). - Test in a private window with extensions off to rule out extension noise.
The "fix" that causes a security problem
When a value is undefined on the server, people often hard-code a key or token into the component so both sides match. If that value is a secret and ends up in client code, it ships to every visitor. Only variables that start with NEXT_PUBLIC_ are sent to the browser, and they must never hold a secret. Keep private keys in server-only code and call them from API routes or server actions.
Also avoid suppressHydrationWarning as a blanket fix. It hides the message, not the cause.
Check what your deployed site ships
After the fix, check what the live site actually sends to the browser. The free read-only check at https://proofforge.live looks at your deployed JavaScript for leaked keys, open Supabase or Firebase data and exposed files, and lists what it could not test. To check that one user cannot read another's data, use the free two-account test: https://proofforge.live/two-account-test
Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.