Nuxt & Universal SSR (50 Qs)•Part 2: Core Architecture & Data Fetching (Mid-Level)
What causes Client Hydration Mismatches in Nuxt 3 and how do you fix them?
senior LevelCLISSR & HydrationSystem Design
In Simple Words: A hydration mismatch occurs when the HTML generated by the Node.js server does not match the Virtual DOM tree produced during the initial client-side render pass. When Vue detects the divergence, it discards or forcefully repaints the DOM, causing flashing and performance degradation.
Common Causes & Fixes:
- Invalid HTML Nesting: Putting a
<div>inside a<p>tag causes browser HTML parsers to auto-close the<p>, breaking Vue's node hierarchy. - Accessing Browser-Only State During Render: Rendering
Date.now(),Math.random(), orwindow.innerWidthdirectly in templates. - Fix: Use
<ClientOnly>for client-only widgets, compute non-deterministic dates on the server and pass viauseState, or initialize browser values insideonMounted().