Web Development

Angular SSR hydration mismatch — server HTML differs from client and interactive bindings fail

Environment

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

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

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

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

Diagnostic steps

  1. 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).
  1. 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.
  1. 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.
  1. 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.
  1. 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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
juliajava Answer
user
Posted 3w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Template-bound non-deterministic values (Date.now, Math.random, locale formatting) produce different server vs client HTML (hypothesis). 9%
Component logic runs differing code paths under isPlatformBrowser checks, causing different node structure (hypothesis). 7%
Server rendered placeholder values while client fetches data and replaces nodes because TransferState not used (hypothesis). 6%

Summary

Hypotheses: (1) Non-deterministic expressions (Date.now(), Math.random(), locale-sensitive formatting) in templates cause differing server/client HTML; (2) Direct DOM or window use in template or component lifecycle runs only client-side leading to mismatch; (3) Async data resolution order differs between server and client; (4) Missing TransferState for server-fetched data causes the client to re-request and re-render.

Suggested Fixes

Diagnostic step 1
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. Result meaning: 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).
Diagnostic step 2
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. Result meaning: If replacing these expressions removes the mismatch, non-determinism was the cause. If not, continue searching for other differences.
Diagnostic step 3
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. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags