Deferrable Views (@defer)
Not everything on a page is needed immediately. A comments section at the bottom, a heavy chart, a map, an editor that opens on click — loading their code up front slows down the first paint. Deferrable views let you mark part of a template so that its components, directives and pipes are downloaded later, when a trigger fires.
The @defer block
@defer (on viewport) {
<app-comments [postId]="post().id" />
} @placeholder {
<p>Comments will load when you scroll here.</p>
} @loading (minimum 300ms) {
<p>Loading comments…</p>
} @error {
<p>Could not load comments.</p>
}| Block | Shown |
|---|---|
@defer |
The real content, once its code has loaded |
@placeholder |
Before the trigger fires (optional minimum time to avoid flicker) |
@loading |
While the chunk is downloading (optional after and minimum times) |
@error |
If loading fails |
Angular's compiler moves every standalone component, directive and pipe used only inside the @defer block into a separate JavaScript chunk. Nothing in your TypeScript changes — you still list them in imports.
Triggers
| Trigger | Fires when |
|---|---|
on idle (default) |
The browser is idle (requestIdleCallback) |
on viewport |
The placeholder scrolls into view |
on interaction |
The user clicks or types in the placeholder |
on hover |
The pointer hovers over the placeholder |
on immediate |
Right after the page renders (non-blocking) |
on timer(2s) |
After a delay |
when condition |
A template expression becomes truthy (once) |
You can also prefetch the code earlier than you render it: @defer (on interaction; prefetch on idle) downloads the chunk when the browser is idle, then shows it instantly on click.
Example: a chart loaded on demand
import { Component, signal } from '@angular/core';
import { SalesChart } from './sales-chart'; // a heavy component
@Component({
selector: 'app-dashboard',
imports: [SalesChart],
template: `
<h3>Q3 overview</h3>
<p>Revenue: {{ revenue() }} USD</p>
@defer (on interaction; prefetch on idle) {
<app-sales-chart />
} @placeholder {
<button class="ph">📊 Show sales chart</button>
} @loading (minimum 400ms) {
<p>Loading chart…</p>
}
`,
styles: `.ph { padding: 24px; width: 100%; border: 2px dashed #ccc; background: #fafafa; }`,
})
export class Dashboard {
revenue = signal(48200);
}The placeholder is what users see first. After a click, the chart appears (or "Loading chart…" for at least 400 ms if the chunk wasn't prefetched yet).
Example: deferring with a condition
when takes any expression. Once it becomes true, the content loads and stays — @defer never goes back to the placeholder:
import { Component, signal } from '@angular/core';
import { MarkdownEditor } from './markdown-editor';
@Component({
selector: 'app-post-editor',
imports: [MarkdownEditor],
template: `
<button (click)="editing.set(true)">✏️ Edit post</button>
@defer (when editing()) {
<app-markdown-editor />
}
`,
})
export class PostEditor {
editing = signal(false);
}How lazy loading works
Under the hood, the deferred chunk is loaded with a dynamic import() — a Promise. The states map directly onto the Promise's life. This plain TypeScript model shows the sequence:
export {};
type State = 'placeholder' | 'loading' | 'complete' | 'error';
async function deferBlock(load: () => Promise<string>, log: (s: string) => void) {
let state: State = 'placeholder';
log(state);
// ...trigger fires...
state = 'loading';
log(state);
try {
const content = await load();
state = 'complete';
log(`${state}: ${content}`);
} catch (e) {
state = 'error';
log(`${state}: ${(e as Error).message}`);
}
}
const fakeChunk = () => new Promise<string>((resolve) => setTimeout(() => resolve('<app-sales-chart>'), 50));
const brokenChunk = () => Promise.reject(new Error('chunk failed to load'));
await deferBlock(fakeChunk, (s) => console.log('chart |', s));
await deferBlock(brokenChunk, (s) => console.log('editor |', s));chart | placeholder
chart | loading
chart | complete: <app-sales-chart>
editor | placeholder
editor | loading
editor | error: chunk failed to load
Good candidates for @defer
- Content below the fold (comments, related articles, footers with widgets)
- Heavy third-party components (charts, maps, rich text editors)
- Features behind an interaction (dialogs, advanced filters)
Note: Components inside
@defermust be standalone and must not be referenced elsewhere in the same file (for example viaviewChild), otherwise they can't be split out and are loaded eagerly.
Tip: Avoid deferring content that is visible on first load with
on immediateoron viewport— it causes layout shifts. Give the placeholder the same size as the final content.
Test yourself
What is the default trigger of a plain @defer { … } block?
- on viewport
- on idle
- on interaction
- on immediate
Which block is shown before the trigger fires?
- @loading
- @error
- @placeholder
- @empty
What does prefetch on idle add to @defer (on interaction; prefetch on idle)?
- It renders the content when idle
- It downloads the code when idle but still renders on interaction
- It disables loading states
- Nothing