Sub topic 3
Hydration Constraints and Diagnosing Mismatches
Assume you know Angular templates, standalone application configuration, and server-side rendering (SSR). Hydration connects Angular behavior to HTML already produced by the server. It works only when the browser can match the server DOM to the client’s expected DOM.
Hydration constraints and mismatches
The server and browser must produce the same structure during the initial render. Common causes of mismatches include:
- Random values or timestamps created during rendering.
- Different data on the server and browser.
- Direct use of
window,document, or browser-only APIs. - Invalid HTML, such as placing a block element inside an element that cannot contain it.
- Direct DOM changes before Angular hydrates the component.
Angular reports hydration warnings in the browser console. Compare the server HTML with the client template, then make the initial state deterministic. Move browser-only work into afterNextRender, and fix invalid element nesting before changing hydration settings.
> callout warning > Treat a mismatch as a correctness problem first. Skipping hydration can hide the warning while increasing browser work.
Skipping hydration for problem components
Use ngSkipHydration on a component host when a third-party widget or legacy component cannot produce stable DOM:
<app-legacy-widget ngSkipHydration />
Angular discards that component’s server-rendered DOM and renders it again in the browser. This is a narrow escape hatch. The component must still be safe to instantiate in the browser, and its server HTML may be replaced after loading.
Replaying events before hydration
With provideClientHydration(withEventReplay()), Angular records supported user events that occur before hydration finishes. It replays those events after the relevant listeners are attached. This helps users who click or type while the page is still becoming interactive, but it does not repair mismatched markup or replace application state validation.
Hydrating deferred blocks on demand
Incremental hydration delays selected components until a trigger occurs:
@defer (hydrate on viewport) { <app-comments /> }
The server can send the comments HTML immediately, while the browser hydrates the block when it enters the viewport. Other triggers can wait for interaction or idle time. This reduces startup work, but delayed content must remain understandable and usable before hydration.
Measuring SSR trade-offs
SSR can improve first content and search indexing, but it adds server rendering time, HTML transfer size, and hydration CPU work. Measure time to first byte (TTFB), largest contentful paint (LCP), interaction to next paint (INP), cumulative layout shift (CLS), and hydration duration. Compare these results with client-side rendering under the same network and device conditions.
Exercise
What should you check first when Angular reports a hydration mismatch?
Show answer
Compare the server and browser DOM, then check for nondeterministic values, invalid HTML, direct DOM changes, and browser-only API use.