Full answer
@defer is a template block that splits the components it contains into a separate JavaScript chunk and loads it later. It gives you lazy loading inside a page, not only between routes.
Syntax and states:
@defer (trigger) { ... }is the main content, shown when loaded.@placeholder (minimum 500ms) { ... }is shown before loading starts.minimumavoids a flash if loading is quick.@loading (after 100ms; minimum 1s) { ... }is shown while the chunk downloads.afterwaits before showing it, so fast loads show no spinner.@error { ... }is shown if the download fails.
Triggers:
on idle(the default): when the browser is idle.on viewport: when the placeholder (or a named element) scrolls into view.on interactionandon hover: when the user clicks or hovers over an element.on immediate: start loading right away, after the first render.on timer(5s): after a delay.when expression: when a condition becomes true. It fires once. Later changes to the condition do not unload the content.
prefetch on idle downloads the code early but shows it only when the main trigger fires.
Rules: only standalone components, directives, and pipes can be deferred. They must not be used anywhere else in the same file outside @defer blocks, for example in another part of the template or as a viewChild type. If they are, they stay in the main bundle. Deferred content is skipped on the server, where only the placeholder renders, unless you use the incremental hydration feature.
Good for: charts, comment sections, maps, and anything below the fold. Do not defer content that shows in the first screen, as it can hurt Largest Contentful Paint and layout stability.
// file: heavy-chart.component.ts
import { Component, input } from '@angular/core';
@Component({
selector: 'app-heavy-chart',
template: `<p>Chart with {{ points().length }} points</p>`,
})
export class HeavyChartComponent {
points = input.required<number[]>();
}
// file: dashboard.component.ts
import { Component, signal } from '@angular/core';
import { HeavyChartComponent } from './heavy-chart.component';
@Component({
selector: 'app-dashboard',
imports: [HeavyChartComponent],
template: `
<h1>Dashboard</h1>
@defer (on viewport; prefetch on idle) {
<app-heavy-chart [points]="marks()" />
} @placeholder (minimum 300ms) {
<div style="height: 200px">Chart appears when you scroll here</div>
} @loading (after 100ms; minimum 500ms) {
<p>Loading chart...</p>
} @error {
<p>The chart could not be loaded.</p>
}
<button #more>Show second chart</button>
@defer (on interaction(more)) {
<app-heavy-chart [points]="[1, 2, 3]" />
} @placeholder {
<p>Press the button above.</p>
}
`,
})
export class DashboardComponent {
readonly marks = signal([55, 62, 71, 80, 77]);
}Angular 20.3. The chart component is a stand-in for something heavy. Add a real charting library inside it.
Key points
- `@defer` creates a lazy chunk for the components inside it.
- Triggers: `idle`, `viewport`, `interaction`, `hover`, `immediate`, `timer`, and `when`.
- `@placeholder`, `@loading`, `@error` handle the visible states. `minimum` and `after` avoid flicker.
- A component also used outside the block stays in the main bundle.
Common mistakes
- Deferring content that is in the first viewport, which delays it and shifts the layout.
- Using `on viewport` with a placeholder that has several root elements. A trigger needs a single element.
- Expecting `when` to hide the content again if the condition becomes false.
Follow-ups
- What renders on the server for a `@defer` block?
- How does `prefetch` differ from the main trigger?
- How would you check that the chart really lives in a separate chunk?
Tips
- Say that you verify with the build output and the network panel.
- Mention layout shift: give the placeholder the same size as the final content.
- Explain the "also used elsewhere" trap in one sentence.
Evaluation rubric
- Names the main triggers and the three state blocks.
- Understands what is deferred and the restrictions.
- Considers performance trade-offs.