Sub topic 11
Hydrating deferred blocks on demand
This chapter assumes you have read the earlier chapters on hydration and event replay. You should know that provideClientHydration lets Angular reuse server-rendered DOM, and that event replay records user events that happen before hydration finishes. You also need @defer blocks, @placeholder, and the on viewport and on idle triggers.
Normal hydration is all-or-nothing. After the page loads, Angular downloads the JavaScript for every component on it and hydrates the whole tree. Incremental hydration changes this. The server renders a @defer block as full HTML. The browser keeps that HTML as inert, "dehydrated" DOM and downloads the block's code only when a trigger fires. The page is visible and readable at once, and you pay for the JavaScript of a heavy section only if the user needs it.
What incremental hydration changes
Without incremental hydration, a @defer block on the server renders only its @placeholder. The browser then downloads the block's code and swaps in the real content. A crawler or a user on a slow connection sees the placeholder, not the content.
With incremental hydration, a @defer block that has a hydrate trigger is handled differently:
- The server renders the block's main content, not the placeholder. The HTML is complete.
- The browser receives that HTML and shows it immediately.
- Angular does not download the block's JavaScript at startup. The block stays dehydrated, and its DOM is not interactive yet.
- When a hydrate trigger fires, Angular downloads the block's chunk and hydrates the existing DOM in place. It reuses the nodes and does not replace them.
The lazy chunk is the same one @defer always produced. The difference is that the user already sees the content, so nothing flashes or shifts when the code arrives.
Enabling it
Incremental hydration is a feature of provideClientHydration. Add withIncrementalHydration() to the config you built in earlier chapters:
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()),
],
};withIncrementalHydration() turns on event replay for you, so you do not need to also list withEventReplay(). Listing both is harmless. Incremental hydration depends on replay because a user can click inside dehydrated content before its code exists. Angular records that click, hydrates the block, and then replays the click.
The feature only does something when the app is server-rendered or prerendered. In a client-only app there is no server HTML to keep dehydrated.
Hydrate triggers
You add a trigger with hydrate on or hydrate when in the @defer parentheses. The triggers mirror the ones you already know, with a few additions.
| Trigger | Hydrates the block when |
|---|---|
| `hydrate on idle` | The browser reports it is idle (`requestIdleCallback`). |
| `hydrate on viewport` | The block scrolls into view. |
| `hydrate on interaction` | The user clicks or presses a key inside the block. |
| `hydrate on hover` | The pointer or keyboard focus enters the block. |
| `hydrate on immediate` | Right after the app finishes loading and hydrating the rest of the page. |
| `hydrate on timer(5s)` | The given time has passed. |
| `hydrate when expr` | The boolean expression becomes true. |
| `hydrate never` | Never. The block stays static HTML for the life of the page. |
A block that uses hydrate on viewport looks like this:
@defer (hydrate on viewport) {
<app-comments [articleId]="id()" />
} @placeholder {
<p>Comments</p>
}The server renders <app-comments> fully. The @placeholder is not used for the server HTML. It still matters when the page is rendered in the browser without server output, for example in a client-rendered route, because there the normal @defer behavior applies.
Combining hydrate triggers with regular triggers
hydrate triggers control hydration of server-rendered HTML. Regular triggers (on idle, on viewport, when) control when the block loads if the browser renders it from scratch. Typical cases are navigating to the route on the client, or a client-rendered route. You can supply both, separated by semicolons:
@defer (on viewport; hydrate on interaction) {
<app-reviews [productId]="id()" />
} @placeholder {
<p>Loading reviews...</p>
}Here the block loads when it scrolls into view during client rendering. In server-rendered HTML it hydrates on the first interaction.
If you write only regular triggers and no hydrate trigger, the block is not hydrated incrementally. The server renders the placeholder and the browser loads the block as it always did.
Choosing a trigger
hydrate on interactionandhydrate on hovergive the best savings for widgets people may never touch, such as a comment form or a size chart. Event replay hides the delay for the first interaction.hoverstarts the download slightly before the click.hydrate on viewportsuits content below the fold that users usually scroll to, and where there is something to update or listen to. It costs the download when the user reaches the block, which may be a moment before they use it.hydrate on idleandhydrate on timerare cheap defaults for blocks that most users will eventually use. They spend spare time on hydration after the important content is ready.hydrate whenties hydration to application state, for example a signal such asisLoggedIn().hydrate neveris for content that has no behavior, such as a legal footer or a static article body. The block's JavaScript is never needed for it. Angular leaves the DOM as the server wrote it.
You can list several hydrate triggers on one block, and the first one to fire wins.
Triggers that hydrate more than the block you touched
Hydration respects the structure of the page. If a block sits inside another dehydrated block, Angular must hydrate the outer one first. Interacting with the inner block triggers hydration of its parents, from the outside in, and then the inner block itself.
@defer (hydrate on viewport) {
<app-product-details />
@defer (hydrate on interaction) {
<app-review-form />
} @placeholder {
<p>Write a review</p>
}
}If a user clicks in the review form before the product details have scrolled into view, Angular hydrates the details block first, then the form, then replays the click. Keep this in mind when you nest blocks. A cheap inner trigger can pull in an expensive outer block.
Hydration also reaches components that are not in a @defer block. If the user interacts with a dehydrated block, anything it depends on must be hydrated too. Angular handles this, but it means the savings are smaller than the number of @defer blocks suggests when blocks are tightly connected.
What event replay does here
Say the user clicks the "Reply" button in a dehydrated comments section. The sequence is:
- A small script that Angular installs early in the page captures the click at the document root.
- Angular matches the click to the dehydrated block and starts hydrating it, downloading its chunk if needed.
- After hydration, Angular replays the captured event on the same element. The
(click)handler runs as though the code had always been there.
Replay covers events such as clicks and key presses. It cannot recover something like a half-typed value in a form field that hydration then re-renders, so avoid relying on state kept only in the dehydrated DOM. Test the first-interaction path on a throttled connection, because the user's action happens after a real download delay.
Making a block ready to be dehydrated
A component inside a hydrate-triggered block follows all the hydration rules from the earlier chapters. The server HTML must match what the component would render in the browser, and the component must not change the DOM by hand before hydration. Incremental hydration adds a few points of its own:
- Keep the block's dependencies inside the block. Angular puts a component into the lazy chunk only if nothing outside the
@deferblock references it. If you import and use<app-comments>elsewhere in the same template, or reference it in non-deferred code, it lands in the main bundle and you lose the savings. - Use standalone components in the block. The usual
@deferrule applies. - Do not depend on browser globals while rendering. Use
afterNextRenderfor browser-only work, as in the platform chapter. That code runs when the block hydrates, not at page load. - Do not combine
ngSkipHydrationwith a deferred block that needs incremental hydration. Skipping hydration discards the server DOM, which is what incremental hydration is meant to keep. Fix the underlying mismatch instead.
A complete example
This article page keeps the body static, hydrates comments when they scroll into view, and hydrates a share widget on first hover.
import { Component, input } from '@angular/core';
import { CommentsComponent } from './comments.component';
import { ShareWidgetComponent } from './share-widget.component';
@Component({
selector: 'app-article-page',
imports: [CommentsComponent, ShareWidgetComponent],
template: `
<article>
<h1>{{ title() }}</h1>
@defer (hydrate never) {
<p>{{ body() }}</p>
}
@defer (hydrate on hover) {
<app-share-widget [url]="url()" />
} @placeholder {
<button type="button">Share</button>
}
@defer (hydrate on viewport; hydrate on timer(10s)) {
<app-comments [articleId]="articleId()" />
} @placeholder {
<p>Comments</p>
}
</article>
`,
})
export class ArticlePageComponent {
title = input.required<string>();
body = input.required<string>();
url = input.required<string>();
articleId = input.required<string>();
}The comments hydrate when they scroll into view, or after ten seconds if that never happens. The <p> with the article body never loads any JavaScript for itself.
Common problems
- Nothing seems deferred. Check that
withIncrementalHydration()is in the providers, that the page is server-rendered, and that the block has ahydratetrigger. Open the browser's network tab and confirm that the block's chunk loads only after the trigger. - The chunk loads at startup. A component from the block is probably used elsewhere in the same file or in an eagerly loaded file, which moves it into the main bundle.
- Hydration mismatch errors inside a block. These come from the same causes as in the hydration chapter: invalid HTML nesting, or direct DOM changes. The error appears when the block hydrates, not at page load, so it can look like a delayed bug.
hydrate whennever fires. The expression must read signals or other values that the block's parent keeps. If it depends on state that only the deferred block holds, nothing can change it.- Layout shift on hydration. This usually means the server DOM and the client render differ, for example because of a client-only value. Compare the server HTML with the hydrated DOM.
Summary
Incremental hydration moves the cost of a section's JavaScript to the moment the user needs it, while the server HTML keeps the content visible and indexable. Enable it with withIncrementalHydration(), mark blocks with hydrate triggers, and match each trigger to how users reach the block. Keep each block's dependencies private to it, so the chunk stays out of the main bundle, and check that the first interaction feels immediate through event replay.
Exercise
A product page has a @defer (on viewport) block around a reviews component. The page is server-rendered and provideClientHydration(withIncrementalHydration()) is configured. Crawlers report that the reviews text is missing from the server HTML. What is wrong, and how do you fix it so the text appears in the HTML and the reviews JavaScript still loads only when the user interacts with them?
Show answer
The block has only a regular trigger, so it is not hydrated incrementally. The server renders its @placeholder and not the reviews, which explains the missing text. Add a hydrate trigger, for example @defer (on viewport; hydrate on interaction). The server then renders the full reviews content, and the browser downloads the chunk and hydrates the block on the first interaction, with event replay re-running the click. The regular on viewport trigger still applies when the page is rendered in the browser.
Exercise
You nest @defer (hydrate on interaction) { <app-reply-form /> } inside @defer (hydrate on viewport) { <app-thread /> }. A user clicks the reply form before the thread has scrolled into view. Which blocks hydrate, in what order, and what happens to the click?
Show answer
Angular hydrates the outer thread block first, then the inner reply form block, because a child cannot hydrate before its parent. After the form is hydrated, Angular replays the captured click on the original element so the form's handler runs.