الانتقال إلى المحتوى
FrontendAngular· 7 دقائق للقراءة

Hydration Isn't Re-rendering: How Angular Reuses Server HTML

The browser runs your components again, but it adopts the DOM the server sent instead of rebuilding it. That only works while both sides produce the same DOM.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A store turns on server-side rendering, and the product page now paints almost instantly. Then the dev console shows NG0500, a hydration node mismatch, pointing at the price component. It adds a "Sale" badge in ngOnInit with createElement() and insertBefore(). Someone puts ngSkipHydration on the root component to silence the error, and it works: no more NG0500. But now the page flickers once as it finishes loading, because that attribute brought back exactly what the team thought SSR always did: rebuild the page from scratch.

The common misconception

The belief: with SSR, the server sends HTML so the user sees something fast, and then the browser throws that HTML away and renders the app again. The server only buys the first paint and the SEO, and what the browser does afterwards is the same as in a client-only app.

There's real evidence for it. The browser still downloads the app's JavaScript and runs every component: a console.log in a constructor shows up once in the server's terminal and again in the browser console. And the belief describes a real mode of Angular SSR. The hydration guide says that without hydration enabled, a server-rendered Angular app "will destroy and re-render the application's DOM", which may cause a visible flicker. So the half-truth is this: your component code runs twice, but the DOM doesn't have to be built twice.

What actually happens

The server sends HTML and a map of it

On the server, Angular renders the page to HTML and annotates it. The NG0500 page describes the browser expecting the DOM structure "as rendered and annotated during server-side rendering". Part of that map lives in the markup itself: Angular's comment nodes act as anchors for view containers such as @if and @for blocks. The page can also carry data, like HttpClient responses the server already fetched, so the browser doesn't have to ask again.

The browser runs your components but adopts the DOM

When the JavaScript arrives, the app bootstraps as usual. Components are created, constructors and ngOnInit run, signals and inputs get their values. The difference is in the step that would normally create elements. In the guide's words, Angular "tries to match existing DOM elements to the application's structure at runtime and reuses DOM nodes when possible". It claims the server's <div> instead of making a new one, and attaches listeners to it. After hydration, a cleanup step removes the nodes nobody claimed.

That's : the same work as a client render, minus building the DOM. The content the user is already looking at stays in place: nothing is torn down and rebuilt, so nothing flickers.

Until then, the page is a picture

Server HTML looks interactive before it is. The guide is explicit that event listeners aren't attached until hydration completes. That's what covers: it records native events such as clicks during that window and replays them once hydration finishes. In Angular 22, event replay comes with incremental hydration, which provideClientHydration() enables by default, so an early click on "Add to cart" isn't lost.

The contract: the same DOM on both sides

Reuse works only if the browser finds exactly what the server annotated, down to whitespace and comment nodes. The guide lists the usual ways to break that:

  • Direct DOM manipulation with native APIs: createElement(), appendChild(), setting innerHTML, moving nodes.
  • Invalid HTML the browser repairs while parsing: a <table> without <tbody>, a <div> inside a <p>, an <a> inside another <a>.
  • Templates that branch on the platform, such as @if with isPlatformBrowser, so the browser renders a different structure than the server did.

Any of these produces a mismatch, NG0500, with the console pointing at the part of the DOM that differs.

TypeScript price.ts
import {  Component, DOCUMENT, ElementRef, inject, input, OnInit,} from '@angular/core';@Component({  selector: 'app-price',  template: `<div class="price">{{ amount() }}</div>`,})export class Price implements OnInit {  readonly amount = input.required<string>();  readonly onSale = input(false);  private readonly host = inject(ElementRef<HTMLElement>).nativeElement;  private readonly doc = inject(DOCUMENT);  ngOnInit() {    if (!this.onSale()) return;    // Angular has no record of this node, so the DOM no longer    // matches what the server annotated: hydration fails (NG0500)    const badge = this.doc.createElement('span');    badge.textContent = 'Sale';    this.host.insertBefore(badge, this.host.firstChild);  }}

The fix isn't a hydration trick; it's moving the badge to where Angular can see it. In the template, the server renders the @if branch, the browser reaches the same branch with the same inputs, and hydration claims the <span> like any other node. When you truly need imperative DOM work, such as a chart library, run it in afterNextRender(), which runs only in the browser after rendering. The guide gives the same advice for third-party scripts that touch the DOM.

ngSkipHydration brings the old behavior back

ngSkipHydration on a component's host makes Angular skip hydrating that component and all its children. The guide says such a component "will destroy and re-render itself", and that putting it on the root component effectively disables hydration for the whole app. It's a last-resort workaround, and the guide treats components that need it as bugs to fix.

MythReality
The browser throws the server HTML awayHydration reuses the existing DOM nodes
Component code runs only on the serverIt runs again in the browser; only DOM creation is skipped
The page is interactive as soon as it showsListeners attach when hydration completes; event replay keeps early clicks
Small server/browser differences are harmlessThe DOM must match; a different structure throws NG0500
ngSkipHydration is a fixIt destroys and re-renders that subtree, flicker included

How to apply it in your project

  1. Confirm hydration is onProjects created with ng new --ssr or ng add @angular/ssr already call provideClientHydration(). In a custom setup, make sure it's in both the browser and the server providers.
  2. Move DOM edits into templatesSearch components for createElement, appendChild, insertBefore and innerHTML. Turn each into template markup, or move it into afterNextRender() when a library must own that DOM.
  3. Validate your HTMLAdd an explicit <tbody> to every table, and look for block elements inside <p> and links inside links.
  4. Treat every ngSkipHydration as a bugList each use, note the component it hides, and remove it once that component stops editing the DOM. Never put it on the root component.

Key takeaways

  • Hydration reuses the server's DOM nodes; without it, Angular destroys and re-renders the page.
  • Your component code still runs in the browser. Only the DOM creation is skipped.
  • Listeners attach when hydration completes; event replay, on by default in Angular 22, keeps early clicks.
  • The server and the browser must produce the same DOM, so edit it through templates, not native APIs.
  • ngSkipHydration is a temporary escape hatch that brings the flicker back for that subtree.

📚 المصادر

  1. Hydration — Angular
  2. Server and hybrid rendering — Angular
  3. NG0500: Hydration Node Mismatch — Angular
  4. provideClientHydration — Angular

التعليقات (0)