Sub topic 4
Enabling hydration and what it reuses
This chapter assumes you know standalone components, ApplicationConfig, dependency injection, and how the browser builds the DOM from HTML. It also assumes your project already renders on the server, as covered in the previous chapters.
The problem hydration solves
Server-side rendering sends finished HTML, so the user sees content quickly. Without hydration, the browser then boots Angular and rebuilds the page. Angular destroys the server-rendered DOM and creates new nodes. The visible result is a flicker, a lost scroll position or focus, and wasted work.
Hydration removes that step. Angular walks the existing DOM, attaches itself to it, and keeps it. The page becomes interactive without being redrawn.
Enabling hydration
You enable hydration with provideClientHydration() in the shared application config. The ng add @angular/ssr command adds it for you. Here is the config, with event replay turned on:
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withFetch()),
provideClientHydration(withEventReplay()),
],
};Put it in the shared config, not the browser-only one. The server config merges this file, so both sides agree that hydration is on. The server then writes extra markers into the HTML, and the browser reads them.
withEventReplay() is a feature argument. It records clicks and other events that happen before hydration finishes and replays them afterward. A later chapter covers it in detail.
What Angular reuses
During server rendering, Angular adds a small amount of metadata to the HTML. It includes an ngh attribute on component host elements and a serialized description of the view structure. In the browser, Angular uses that metadata to match each part of the component tree to an existing DOM node.
Angular reuses these things:
- DOM nodes created by templates, including elements and text.
- The structure of embedded views, such as
@if,@for, and*ngIfblocks. - Component boundaries, so each component instance connects to its host element.
Angular does not reuse application state. Component classes, services, and signals run again in the browser. Hydration saves DOM creation. It does not save JavaScript execution. State that the server fetched has to be transferred separately, which is the topic of a later chapter.
What happens at startup
The page is visible after the first paint, but it is not interactive until Angular attaches listeners. This gap is why event replay exists.
Confirming that it works
Run the app and open the browser console. In development mode, Angular logs a message such as "Angular hydrated 12 component(s) and 87 node(s)". If you see no message, hydration is not active.
You can also inspect the page source. Host elements should carry ngh attributes. Another check is to put a breakpoint in a component, then see whether the DOM node is the same object before and after bootstrap. If hydration works, it is.
Common errors
- Hydration enabled only in the browser config. The server omits the markers, and Angular falls back to a full re-render. Put the provider in the shared config.
- Mismatch warnings. The server DOM and the client's expected DOM differ. Typical causes are invalid HTML nesting and direct DOM manipulation. A later chapter covers diagnosis.
- Expecting state to carry over. A value set during server rendering is gone in the browser unless you transfer it.
- Components that touch the DOM directly. Code that uses
innerHTMLor moves nodes can break the match between Angular's view description and the real DOM.
Exercises
Exercise
A teammate adds provideClientHydration() to a browser-only config file that the server config does not import. The app still renders on the server. What goes wrong, and how do you fix it?
Show answer
The server HTML has no hydration markers, so the browser cannot match components to existing nodes. Angular discards the server DOM and renders again, which brings back the flicker. Move the provider into the shared appConfig, which the server config merges.
Exercise
After hydration, a component's field still holds its initial value, even though the server set it to something else during rendering. Why?
Show answer
Hydration reuses DOM nodes, not component state. The component class runs again in the browser and initializes from scratch. Transfer the server's data with the HTTP transfer cache or TransferState.