
Incremental Hydration: Pages Don't Have to Hydrate All at Once
- كاتب موثّق
- Angular 22
محتويات المقال
@defer (on viewport). The bundle shrinks, but the server-rendered HTML now has gray skeletons where the reviews used to be, and the page can shift when the real content replaces them.
The common misconception
@defer guide says that by default, when rendering on the server, defer blocks "always render their @placeholder" and their triggers aren't invoked. The belief simply stops at those defaults.
What actually happens
A hydrate trigger turns @defer into a hydration boundary
@defer. According to the guide, a hydrate trigger tells Angular to "load that defer block's dependencies during server-side rendering and render the main template rather than the @placeholder". So the server's HTML contains the real reviews.
The trigger downloads, hydrates, then replays
It's already on in Angular 22
provideClientHydration() enables incremental hydration by default. withIncrementalHydration() is deprecated, with removal planned for v24, and withNoIncrementalHydration() opts out. Content without a hydrate trigger hydrates as before, so nothing changes until you add triggers.
import { Component } from '@angular/core';@Component({ selector: 'app-product-page', imports: [ProductSummary, ProductSpecs, Reviews], template: ` <app-product-summary /> <!-- The server renders only the skeleton here, and the page shifts when the real reviews replace it in the browser --> @defer (on viewport) { <app-reviews /> } @placeholder { <div class="reviews-skeleton"></div> } <!-- Static text, but its code ships and hydrates up front --> <app-product-specs /> `,})export class ProductPage {}import { Component } from '@angular/core';@Component({ selector: 'app-product-page', imports: [ProductSummary, ProductSpecs, Reviews], template: ` <app-product-summary /> <!-- Rendered on the server; code loads when it's scrolled to --> @defer (on viewport; hydrate on viewport) { <app-reviews /> } @placeholder { <div class="reviews-skeleton"></div> } <!-- Stays static on the first load: its code isn't downloaded --> @defer (on idle; hydrate never) { <app-product-specs /> } @placeholder { <div class="specs-skeleton"></div> } `,})export class ProductPage {}hydrate never keeps them as static HTML.
Choosing a trigger
hydrate on immediate | ||
hydrate on idle | requestIdleCallback) | |
hydrate on viewport | ||
hydrate on interaction or hover | ||
hydrate when | ||
hydrate never |
Hydrate triggers only cover the first load
@placeholder is rendered first. That's why the example keeps both kinds of trigger and the placeholders, and why the guide says you still need a @placeholder. Nested blocks hydrate from the top-most dehydrated parent down, and hydrate never on a block keeps its whole subtree static.
How to apply it in your project
Check your hydration config Keep provideClientHydration()withoutwithNoIncrementalHydration(). Remove the deprecatedwithIncrementalHydration(), andwithEventReplay(), which incremental hydration already enables.Find heavy sections below the fold Look for large components users rarely reach on the first screen: reviews, recommendations, comments and long tables. Add a hydrate trigger to each one Wrap each section in @deferwith a regular trigger for client navigation and a hydrate trigger for the first load, and keep a@placeholderwhose size matches the content.Test both paths Load the page directly to exercise the hydrate trigger, then reach it through a routerLinkto exercise the regular trigger and the placeholder.
Key takeaways
Full hydration hydrates the page as one tree; incremental hydration splits it at @deferblocks with hydrate triggers.With a hydrate trigger, the server renders the block's real content, and the browser keeps it as static HTML until the trigger fires. Event replay queues clicks on dehydrated content and replays them after hydration. In Angular 22, provideClientHydration()enables incremental hydration and event replay by default.Hydrate triggers cover the initial load only; keep a regular trigger and a @placeholderfor client-side navigation.
التعليقات (0)