Sub topic 8
Hydration constraints and diagnosing mismatches
This chapter assumes you have enabled hydration with provideClientHydration and understand that Angular reuses the server-rendered DOM instead of rebuilding it. You should also know how the browser parses HTML into a DOM tree.
Why mismatches happen
During hydration, Angular walks its component tree and expects to find the DOM nodes that the server produced. It attaches listeners and state to those nodes. This only works if the DOM Angular finds has the same structure as the DOM it would have created itself.
A mismatch happens when the two structures differ. Angular then cannot reuse the node. In development it logs an error such as NG0500 (unexpected node) or NG0501 (missing node), and the hydrated page may flicker or lose content.
There are two common causes:
- The HTML in your templates is invalid, so the browser's parser changes the structure.
- Code changes the DOM directly, so the DOM no longer matches what Angular expects.
Cause 1: invalid HTML
The server sends HTML as text. The browser parses that text and repairs invalid markup by moving or inserting elements. After that repair, the DOM no longer matches the component tree Angular built on the server.
Common examples:
| Template | What the browser does |
|---|---|
| `<table>` with `<tr>` but no `<tbody>` | Inserts a `<tbody>` that Angular did not create |
| `<p>` containing a `<div>` | Closes the `<p>` before the `<div>` |
| `<a>` nested inside another `<a>` | Closes the first link before the second |
| `<ul>` containing something other than `<li>` | Moves or wraps the content |
Here is a component that causes a mismatch:
import { Component } from '@angular/core';
@Component({
selector: 'app-bad-paragraph',
template: `<p><div>Block inside a paragraph</div></p>`,
})
export class BadParagraphComponent {}The <div> cannot live inside a <p>, so the parser ends the paragraph early. The fix is to use valid nesting:
import { Component } from '@angular/core';
@Component({
selector: 'app-good-paragraph',
template: `<div><p>Text inside a paragraph</p></div>`,
})
export class GoodParagraphComponent {}Cause 2: direct DOM manipulation
Code that edits the DOM with innerHTML, appendChild, insertBefore, or document.createElement changes nodes behind Angular's back. If that code runs on the server, the output contains nodes Angular did not create. If it runs in the browser before hydration, it breaks the structure Angular is about to match.
import { Component, ElementRef, inject } from '@angular/core';
@Component({
selector: 'app-risky',
template: `<div>Content</div>`,
})
export class RiskyComponent {
private el = inject(ElementRef);
constructor() {
// Runs during server rendering and changes the DOM.
this.el.nativeElement.innerHTML = '<span>Injected</span>';
}
}Prefer template bindings, which Angular tracks. When you must touch the DOM, do it inside afterNextRender, which runs only in the browser and after hydration has finished. The WidthBoxComponent from the previous chapter shows this pattern.
Third-party libraries are a frequent source of this problem. A chart or editor library that builds its own DOM inside a component host will cause mismatches.
How to diagnose a mismatch
Follow these steps:
- Run the app in development mode. Angular reports hydration errors only there.
- Open the browser console and read the error. It names the component and often shows the expected and actual DOM.
- Check the template for the invalid patterns in the table above.
- Search the component and its libraries for direct DOM calls.
- If the cause is still unclear, compare View Page Source (the server HTML) with the Elements panel (the live DOM). The difference points to the node that changed.
Hydration also relies on components keeping stable output between server and browser. Content that differs by environment, such as a timestamp from new Date(), can produce text that does not match. Compute such values in a way that gives the same result on both sides, or set them after render.
Common mistakes
- Putting
<tr>elements directly under<table>in a template. - Wrapping block elements in
<p>. - Using
ElementRef.nativeElementto change structure in a constructor orngOnInit. - Testing only in production builds, where the hydration errors are not shown.
Exercise
A component template contains <table><tr><td>{{ name }}</td></tr></table>. The console reports an NG05xx hydration error in development. What is the likely cause, and how do you fix it?
Show answer
The browser inserts a <tbody> between <table> and <tr> when it parses the server HTML, so the DOM has a node Angular did not create. Add the <tbody> yourself: <table><tbody><tr><td>{{ name }}</td></tr></tbody></table>.
Exercise
A component sets innerHTML on its host element inside ngOnInit, and hydration reports a mismatch. Where should the DOM code go instead?
Show answer
Move it into afterNextRender, which runs only in the browser after hydration. Better still, replace it with a template binding so Angular controls the nodes.