Next.js & App Router (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Diagnosing and Eliminating Hydration Mismatches.
senior LevelSSR & HydrationSystem Design
In Simple Words: Occurs when the server-rendered HTML diverges from the initial client render:
- Common culprits:
- Using
typeof window !== 'undefined'to conditionally render HTML. - Rendering non-deterministic values:
new Date(),Math.random(). - Invalid HTML nesting (e.g.
<p><div>Text</div></p>or<table><tr>). - Browser extensions injecting DOM nodes (e.g. password managers).
- Using
- Fixes: Use
suppressHydrationWarningon specific nodes, or defer rendering client-only values until mounted viauseEffectwithhasMountedflag.