Angular SSR hydration mismatch — server HTML differs from client and interactive bindings fail
Environment
Angular 14 app using @nguniversal/express-engine for SSR. Server runs Node 18 on Linux, client bundle via Angular CLI. Some components render dates, random IDs, and use window/document directly in templates.
Symptoms
Page initially shows server-rendered content, but on client boot the DOM updates (flashes) and console shows hydration-related warnings. Interactive controls (click handlers, *ngIf conditions) sometimes fail to attach or behave inconsistent with server HTML.
Observations
Server-rendered HTML contains formatted Date strings and unique IDs from Math.random(). The client when booting computes different values, causing element attribute mismatches and removal/creation of DOM nodes. No SSR-specific errors in server logs, but client console logs show 'Hydration failed' or frequent reflows.
Already attempted
Wrapped some Date formatting in isPlatformBrowser checks, tried TransferState for some data, but mismatches persist. Tried disabling Zone.js for SSR to test determinism; partial improvement only.
Diagnostic steps
- Save the exact server-rendered HTML (view-source: or curl the SSR endpoint) and, immediately after client boot, dump document.documentElement.outerHTML in the browser console. Diff the two outputs.
- What the result means: If serialized HTML differs (timestamps, IDs, text nodes), identify which template expressions produced the difference. If identical but behavior diverges, issue is likely runtime-only (event binding).
- Search templates and components for uses of Date, Math.random, Intl.DateTimeFormat or direct window/document access and temporarily replace with deterministic constants to test.
- What the result means: If replacing these expressions removes the mismatch, non-determinism was the cause. If not, continue searching for other differences.
- Instrument server rendering to include a hash of rendered root HTML (e.g., sha1) and have client compute same hash early in bootstrap to compare determinism.
- What the result means: If hashes differ, server/client output differs; the diff can point to offending nodes. Matching hashes mean initial DOM matches and problem occurs later during hydrating bindings.
- Enable TransferState for server-fetched async data and ensure client reads TransferState instead of re-fetching on bootstrap. Compare network panel to ensure client skips requests.
- What the result means: If client still requests data, TransferState wasn't wired; that will cause re-rendering and mismatch. If client doesn't request, data parity likely OK.
- Temporarily stub third-party components (replace with no-op) and retry SSR hydrate.
- What the result means: If mismatch disappears, library mutates DOM or uses browser-only APIs causing differences; update or lazy-load such components.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
How do I systematically find template-side non-determinism and ensure server and client produce identical static HTML for hydration? What checks prove the root cause?
Verified community troubleshooting
Start by proving whether server and client HTML are different: fetch the SSR HTML (curl) and, after client bootstrap, run document.documentElement.outerHTML and diff them. If you find Date, Math.random, or locale format differences, those are the usual culprits. Use TransferState to serialize any server-fetched data so the client doesn't re-request and re-render. For third-party UI libs, temporarily replace them with no-ops to see if hydrating bindings change. To verify a successful fix, run the hash comparison in CI: server compute hash at render, client computes same hash on boot — identical means deterministic output and removes the primary cause of hydration failure.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags