Sub topic 11
Hydrating deferred blocks on demand
Full hydration makes the browser download and run the code for every component on the page, even parts the user may never touch. Incremental hydration avoids this. The server still renders the whole page, but the browser hydrates chosen @defer blocks only when a trigger fires.
This chapter assumes you have read the earlier chapters on hydration basics and event replay. You should also know the @defer block syntax and how provideClientHydration is configured.
What changes with incremental hydration
Without it, a @defer block on the server renders only its @placeholder. With incremental hydration, the server renders the real content of the block. The browser keeps that HTML as static markup and leaves it inert. The block's JavaScript is fetched and the block becomes interactive only when its hydrate trigger fires.
Enabling it
Add withIncrementalHydration() to the hydration provider. It builds on event replay, so it turns that feature on for you.
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import {
provideClientHydration,
withIncrementalHydration,
} from '@angular/platform-browser';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withFetch()),
provideClientHydration(withIncrementalHydration()),
],
};Adding a hydrate trigger
You add a hydrate trigger to a @defer block. This block is server-rendered and hydrates when it scrolls into view:
@defer (hydrate on viewport) {
<app-comments />
} @placeholder {
<p>Comments</p>
}The @placeholder is still used when the page is rendered entirely in the browser, for example on client-side navigation. On the initial server-rendered load, the browser sees the real comments markup.
The available hydrate triggers are:
| Trigger | Hydrates when |
|---|---|
| `hydrate on idle` | the browser is idle |
| `hydrate on viewport` | the block enters the viewport |
| `hydrate on interaction` | the user clicks or presses a key in the block |
| `hydrate on hover` | the pointer hovers over the block |
| `hydrate on immediate` | right after the page is hydrated |
| `hydrate on timer(5s)` | the given time has passed |
| `hydrate when condition` | the expression becomes true |
| `hydrate never` | never, so the block stays static |
You can combine several triggers with semicolons. The first one to fire wins.
@defer (hydrate on interaction; hydrate on timer(10s)) {
<app-reviews [productId]="id()" />
}Interaction triggers rely on event replay
When a user clicks inside a dehydrated block, the click happens before any listener exists. Event replay records it. Angular then hydrates the block and replays the click, so the user's action is not lost. This is why hydrate on interaction and hydrate on hover work without a missed first click.
Nested blocks hydrate from the outside in
If a deferred block sits inside another deferred block, a child cannot hydrate before its parent. Triggering the child hydrates the parent first, then the child. Keep this in mind when you nest blocks. A cheap inner trigger can pull in an expensive outer block.
Static content with `hydrate never`
Use hydrate never for content that is never interactive, such as a legal footer or a long article body. The HTML ships from the server and no JavaScript for that block is ever downloaded or run.
@defer (hydrate never) {
<app-legal-footer />
}Common errors
- Nothing hydrates lazily. You used
hydratetriggers but did not addwithIncrementalHydration(). Without it, the triggers have no effect. - Confusing the two kinds of trigger.
@defer (on viewport)controls when the browser loads and renders the block on the client.@defer (hydrate on viewport)controls when server-rendered HTML becomes interactive. You can use both on one block. - A
whencondition that depends on state not set yet. The expression is evaluated in the browser. Make sure it can become true without user input from inside the block itself. - Interactive controls in a
hydrate neverblock. Buttons and inputs there will not respond, because no listeners are ever attached.
Exercise
Exercise
A product page has a comments section marked @defer (hydrate on viewport). A user scrolls quickly and clicks a "Reply" button in the comments the moment they appear. Why is the click not lost, and which feature makes this possible?
Show answer
Event replay. The browser records the click before the comments block is hydrated. After the block hydrates, Angular replays the recorded event on the new listener. withIncrementalHydration() enables event replay automatically.
Exercise
You want a footer with plain links to ship from the server but never cost any JavaScript on the client. Which block do you write?
Show answer
@defer (hydrate never) { <app-footer /> }. The server renders the footer, and the browser keeps it as static HTML without downloading its code.