Sub topic 9
Skipping hydration for problem components
This chapter assumes you have SSR and hydration enabled with provideClientHydration. It also assumes you know what a hydration mismatch is, from the previous chapter. You should be comfortable with standalone components and template syntax.
What skipping hydration does
Hydration reuses the DOM that the server rendered. It walks the existing nodes and attaches them to your component instances. This works only if the DOM in the browser matches what Angular expects.
Some components break that assumption. A third-party widget may rewrite its own markup. A legacy component may build its DOM by hand. Angular then reports a mismatch and, in the worst case, the page shows broken or duplicated content.
The ngSkipHydration attribute is an escape hatch. You put it on a component's host element. Angular then does two things for that component and everything inside it:
- It discards the server-rendered DOM for that subtree.
- It renders the subtree from scratch in the browser, as in plain client-side rendering.
The rest of the page still hydrates normally.
Using the attribute
Add the attribute in the template of the parent component, on the element that hosts the problem component.
<app-legacy-widget ngSkipHydration />You can also set it on the component itself, through a host binding. This way every use of the component skips hydration, and callers do not need to remember the attribute.
import { Component } from '@angular/core';
@Component({
selector: 'app-legacy-widget',
host: { ngSkipHydration: 'true' },
template: `<div class="legacy-root"></div>`,
})
export class LegacyWidgetComponent {}The attribute works only on a component's host element. Placing it on a plain element such as a <div> produces a warning, and Angular ignores it.
What happens during the swap
The server still renders the component. The browser receives that HTML, shows it, and then replaces it once Angular bootstraps.
The diagram below shows the order of events.
Because the DOM is replaced, users may see a flicker. Any state held in the old DOM, such as typed text or scroll position inside the widget, is lost.
Rules and limits
- Skipping is per subtree. All children of the marked component are also rendered on the client only.
- Setting it on a component that holds lots of content throws away most of the benefit of SSR for that part of the page.
- Do not put it on the root component. Angular ignores the attribute there, because it would mean no hydration at all.
- Skipped components still run on the server. If the component touches
windowordocument, it still needs the guards from the platform APIs chapter.
Direct DOM manipulation is a common reason to skip. Before you do, check whether the code can move into afterNextRender. That fixes the cause and keeps hydration working for the component.
import { Component, ElementRef, afterNextRender, viewChild } from '@angular/core';
@Component({
selector: 'app-width-box',
template: `<div #host>Measuring...</div>`,
})
export class WidthBoxComponent {
private host = viewChild.required<ElementRef<HTMLDivElement>>('host');
constructor() {
afterNextRender(() => {
this.host().nativeElement.textContent = `Window width: ${window.innerWidth}px`;
});
}
}When to use it
Use ngSkipHydration when you cannot change the code that causes the mismatch. Typical cases are a third-party library, an old component that rewrites its own DOM, or markup produced by innerHTML that the browser reshapes.
Do not use it to silence a mismatch you could fix. Invalid HTML nesting, for example a <div> inside a <p>, is better corrected in the template.
| Situation | Better choice |
|---|---|
| Invalid HTML nesting in your own template | Fix the template |
| Your code reads `window` or edits the DOM | Move it into `afterNextRender` |
| Third-party widget that rebuilds its DOM | `ngSkipHydration` |
| Legacy component you cannot change | `ngSkipHydration` |
Check the result
Open the page in development mode and watch the browser console. Mismatch errors for the skipped component should disappear. Then reload with a throttled network and watch for the flicker when the widget is replaced. If the flicker is distracting, consider giving the host element a fixed height so the layout does not jump.
Exercise
A teammate sets ngSkipHydration on a <div> that wraps a chart library, and the mismatch errors continue. What are two things wrong, and what would you do instead?
Show answer
First, the attribute only works on a component's host element, so on a plain <div> Angular ignores it. Second, the chart is likely drawing into the DOM directly, which can often be fixed by drawing inside afterNextRender. Put the chart in its own component and either move the drawing code into afterNextRender, or, if the library cannot be changed, put ngSkipHydration on that component's host element.
Exercise
What happens to the server-rendered HTML of a component marked with ngSkipHydration, and what is the cost to the user?
Show answer
Angular discards it and renders the component, and all its children, from scratch in the browser. The user may see a flicker when the content is replaced, and loses any state held in the old DOM.