Sub topic 13
Deferred blocks with server-side rendering
This chapter covers what a @defer block does when Angular renders your page on the server, and what the browser does with that HTML afterward. It assumes you know standalone components, the @defer triggers and sub-blocks from earlier chapters, and the basics of Angular SSR (ng add @angular/ssr, provideClientHydration). Basic familiarity with how hydration reuses server-rendered DOM also helps.
What happens on the server by default
During server-side rendering (SSR), Angular produces an HTML string and stops. It does not wait for user input, viewport changes, or timers. Deferred blocks follow the same rule:
- Angular renders the
@placeholderblock. If you wrote no placeholder, the block renders nothing. - Angular never renders
@loadingor@erroron the server. - No trigger runs.
on viewport,on interaction,on hover,on idle,on timerandwhenare all ignored. - No lazy chunk is requested, and the deferred dependencies are not executed on the server.
Take this template:
<h1>{{ product.name }}</h1>
@defer (on viewport) {
<app-reviews [productId]="product.id" />
} @placeholder {
<div class="reviews-placeholder">Reviews load as you scroll.</div>
} @loading {
<app-spinner />
}The HTML sent to the browser contains the <h1> and the placeholder <div>. It contains neither <app-reviews> nor <app-spinner>.
Rendering only the placeholder is deliberate. The server cannot know whether a visitor will scroll to the reviews, so it renders the state every visitor starts in.
What happens in the browser
After the HTML arrives, the Angular bundle loads and the app bootstraps on the client. With hydration enabled, Angular reuses the server-rendered DOM instead of rebuilding it. For a deferred block, the placeholder DOM is reused, and the block then behaves as it would in a client-only app:
- The client registers the block's triggers.
- When a trigger fires, Angular fetches the chunk.
- Angular swaps the placeholder for
@loading(if the load is slow enough), then for the main content.
The result is a fast first paint, but the deferred content is missing from the initial HTML. Two consequences follow.
Search engines and link previews
Crawlers that read only the raw HTML will see the placeholder, not your reviews, comments or related-product list. Do not defer content you need indexed. A page's main text, headings and the data behind structured metadata should stay in the eager part of the template.
Layout shift
The placeholder is part of the server HTML, so the browser lays it out before any JavaScript runs. If the real content is much taller than the placeholder, the swap pushes content below it down. That counts against Cumulative Layout Shift (CLS). Give the placeholder the same dimensions as the final content:
@defer (on viewport) {
<app-reviews [productId]="product.id" />
} @placeholder {
<div class="reviews-placeholder" style="min-height: 480px"></div>
}Because @placeholder is what the server renders, it is also what first-time visitors see. Treat it as real UI.
Behavior on the server for each trigger
Triggers do nothing on the server, but they still matter for what the client does later. This table summarizes the behavior without incremental hydration.
| Part of the block | On the server | In the browser after hydration |
|---|---|---|
| `@placeholder` | Rendered into the HTML | Reused, then replaced when loading starts |
| `@loading` | Not rendered | Shown if loading exceeds the `after` delay |
| `@error` | Not rendered | Shown if the chunk fails to load |
| Main content | Not rendered | Rendered after the trigger fires and the chunk loads |
| `on` triggers | Ignored | Active |
| `prefetch on` and `prefetch when` | Ignored | Active, so chunks can download early |
| `when` condition | Not evaluated to start loading | Evaluated; loading starts the first time it is true |
A when condition that is already true when the page hydrates starts loading right after hydration, so the visitor sees the placeholder briefly before the content appears. A hydrated page is also the point where on idle becomes meaningful, since idle means the browser's idle period after the app has started.
Incremental hydration
Rendering only the placeholder on the server has a cost. Content below the fold, which is exactly what you would defer, is absent from the HTML. Incremental hydration addresses this. The server renders the main content of the deferred block, and the client hydrates that content only when a trigger fires. It is stable from Angular 20.
Enable it with the withIncrementalHydration feature:
import { ApplicationConfig } from '@angular/core';
import {
provideClientHydration,
withIncrementalHydration,
} from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideClientHydration(withIncrementalHydration())],
};Then add a hydrate trigger to the block:
@defer (hydrate on viewport) {
<app-reviews [productId]="product.id" />
} @placeholder {
<div class="reviews-placeholder" style="min-height: 480px"></div>
}Now the server HTML contains the real <app-reviews> markup, so crawlers and users with slow JavaScript still see it. The browser does not download or run the component code until the block scrolls into view. At that point Angular fetches the chunk and attaches the component to the existing DOM instead of replacing it.
The hydrate keyword supports the same trigger family you already know:
hydrate on idle,hydrate on immediate,hydrate on timer(2s)hydrate on viewport,hydrate on interaction,hydrate on hoverhydrate when conditionhydrate never, which keeps the block as static server HTML and never hydrates it
You can combine several, separated by semicolons, and add a regular on trigger for client-side rendering:
@defer (on viewport; hydrate on interaction; prefetch on idle) {
<app-chart [data]="data" />
} @placeholder {
<div class="chart-placeholder" style="height: 320px"></div>
}Here hydrate on interaction applies when the block was server-rendered. on viewport applies when the block is created fresh on the client, for example after navigating to the page in the browser. prefetch on idle works in both cases.
Event replay
Server-rendered content that is not yet hydrated is not interactive. If a visitor clicks a button inside a dehydrated block, Angular records the event, hydrates the block, and replays the event once the component is ready. Enabling withIncrementalHydration turns this event replay on for you. This is why hydrate on interaction and hydrate on hover feel natural: the first click starts hydration and is not lost.
Rules and restrictions
- A
hydratetrigger only works with SSR andwithIncrementalHydration. In a client-only app, hydrate triggers are ignored and the regularontriggers apply. - When a deferred block sits inside another, Angular hydrates the parent first. A child block cannot be hydrated before its parent.
- A block with
hydrate nevercannot respond to events. Use it only for content that needs no interactivity. @loadingand@errorare not rendered on the server even with incremental hydration. They still appear in the browser if the chunk load is slow or fails.
Choosing between the two approaches
| Question | Default (placeholder only) | Incremental hydration |
|---|---|---|
| Is the content in the first HTML? | No | Yes |
| Does a crawler see it? | No | Yes |
| Is the server response larger? | No | Yes, it includes the deferred markup |
| Is the server render slower? | No | Possibly, as it renders more |
| Layout shift risk | Placeholder must match content size | Low, since real markup is already laid out |
| Setup | None beyond SSR and hydration | `withIncrementalHydration()` and `hydrate` triggers |
Use the default for content that does not need to be indexed and is cheap to replace, such as a chat widget or a cookie settings panel. Use incremental hydration for content that is part of the page's meaning but expensive in JavaScript, such as reviews, comments or a long product list.
Common mistakes
- Putting essential, indexable text in a plain
@deferblock and finding it missing from "view source". - Writing a tiny placeholder for a tall component, which causes layout shift on the hydrated page.
- Expecting
@loadingto appear in the server HTML. It never does. - Testing SSR output only in the browser. Check the raw HTML with
curlor the Network tab's response view to see what the server sent. - Using
hydrate ontriggers in an app that has hydration disabled.
Exercise
A product page uses @defer (on viewport) { <app-reviews /> } @placeholder { <div style="height: 40px"></div> }. The real reviews list is about 600px tall. Name two problems this causes with SSR, and give one fix for each.
Show answer
First, crawlers and no-JavaScript users never see the reviews, because the server renders only the placeholder. Fix: use incremental hydration with @defer (hydrate on viewport) and withIncrementalHydration(), so the server renders the reviews markup. Second, when the chunk loads the 40px placeholder is replaced by 600px of content, which shifts the layout and raises CLS. Fix: give the placeholder a min-height close to the real content's height, such as 600px.
Exercise
You use @defer (on viewport; hydrate on interaction) with incremental hydration. A visitor loads the server-rendered page and clicks a button inside the deferred block before any code for it has loaded. What happens to the click, and what does on viewport do in this case?
Show answer
Angular records the click, hydrates the block (downloading its chunk and attaching the component to the existing DOM), then replays the click so it is not lost. on viewport does nothing for this server-rendered block, since hydrate triggers govern hydration. It applies only when the block is rendered on the client, for example after in-app navigation.