Menu

Next.js "Hydration failed because the initial UI does not match": 6 causes, the fix, and the one that leaks a key
📰

Next.js "Hydration failed because the initial UI does not match": 6 causes, the fix, and the one that leaks a key

DEV Community: nextjs·ProofForge·3 days ago
#w6askkCr
#devtonextjs#devto#dev#server#browser
Long article (476 words) — no AI summary yet.
Reading 0:00
15s threshold

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.

Enter fullscreen mode

Exit fullscreen mode

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 , navigator or document used while rendering give different results on the server and client.

Time and randomness. new Date() , Date.now() or Math.random() in the render output differ between the two renders.

Invalid HTML nesting.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.