Sub topic 13
Deferred blocks with server-side rendering
This chapter assumes you know how @defer triggers and the @placeholder block work. It also assumes your app already uses Angular SSR (server-side rendering) with hydration. Hydration is the step where the browser reuses the server-rendered HTML instead of rebuilding it.
What the server does with @defer
The server has no viewport, no mouse and no user to click anything. Because of this, Angular does not run triggers during server rendering. It does not run on idle, on viewport, on interaction, on hover, on immediate or on timer. It also does not run when conditions or any prefetch triggers.
By default, the server renders only the @placeholder block. If you wrote no placeholder, it renders nothing for that block. The @loading and @error blocks never appear in server output.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-placeholder"></div>
}The server sends the empty div. The browser hydrates the page, and only then do the triggers start. When the placeholder scrolls into view, the browser loads the chunk and swaps in <app-comments>.
Consequences of placeholder-only output
- Search engines and first paint. Content inside the main block is missing from the server HTML. Do not defer content that must be indexed or visible immediately, such as the main article text.
- Layout shift. The placeholder is the only thing the server reserves space for. If the deferred content is taller than the placeholder, the page jumps when it arrives. Give the placeholder the same dimensions as the final content.
- Timer and loading blocks. Because
minimumandaftertimings run in the browser, they start after hydration, not during the server render.
Rendering the main content on the server
Angular's incremental hydration lets the server render the main block and the browser hydrate it later. You enable it in the application config and add a hydrate trigger to the block.
import { ApplicationConfig } from '@angular/core';
import {
provideClientHydration,
withIncrementalHydration,
} from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideClientHydration(withIncrementalHydration())],
};@defer (hydrate on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-placeholder"></div>
}With a hydrate trigger, the server renders <app-comments> into the HTML, so the content is visible and indexable. The browser leaves that HTML inert and does not download the component's code until the trigger fires. After that, it hydrates the existing DOM instead of replacing it, so there is no placeholder swap and less layout shift.
The hydrate triggers mirror the regular ones:
| Trigger | Hydrates the block when |
|---|---|
| `hydrate on idle` | The browser is idle |
| `hydrate on immediate` | Hydration of the page finishes |
| `hydrate on timer(5s)` | The delay has passed |
| `hydrate on viewport` | The block scrolls into view |
| `hydrate on interaction` | The user clicks or presses a key in it |
| `hydrate on hover` | The pointer hovers over it |
| `hydrate when expr` | The expression becomes true |
| `hydrate never` | Never. The block stays static HTML |
With hydrate on interaction, Angular replays the user's click after hydration finishes, so the first click is not lost.
Common mistakes
- Expecting
on timerorwhento run on the server. They run only in the browser. - Leaving out
@placeholderand then seeing the content pop in from nothing after hydration. - Using
hydrate neveron a block that has interactive controls. The controls will not respond. - Deferring above-the-fold content. The server sends only a placeholder, so the page looks incomplete until the chunk arrives.
Exercise
A page uses @defer (on viewport) { <app-reviews /> } with no placeholder, and the page is server-rendered without incremental hydration. What does the server HTML contain for the reviews, and how would you make the reviews appear in the server HTML?
Show answer
The server HTML contains nothing for the block, because triggers do not run on the server and there is no placeholder to render. To get the reviews into the server HTML, enable withIncrementalHydration() in provideClientHydration and change the block to @defer (hydrate on viewport). The server then renders <app-reviews />, and the browser hydrates it when it scrolls into view.