Sub topic 3
What can be deferred: standalone dependencies and eager references
This chapter assumes you can write standalone components with an imports array, use @if and @for in templates, and read the file list from an Angular CLI production build. It explains which template dependencies the compiler can move into a lazy chunk, and which ones stay in the main bundle even when you wrap them in @defer.
What the compiler actually defers
A @defer block does not lazy-load a template fragment. It lazy-loads the dependencies the fragment uses. The compiler looks at the components, directives and pipes referenced inside the block. It replaces their static import statements with dynamic import() calls, and the bundler puts the result in a separate chunk.
You write no import() call yourself. You keep the normal import line and the normal imports array entry:
import { Component } from '@angular/core';
import { CommentsComponent } from './comments.component';
@Component({
selector: 'app-post',
imports: [CommentsComponent],
template: `
<article>...</article>
@defer (on viewport) {
<app-comments />
} @placeholder {
<div class="comments-placeholder"></div>
}
`,
})
export class PostComponent {}The compiler sees that CommentsComponent is used only inside the @defer block. It removes the eager import and generates a function that loads the class on demand. This works for every dependency kind that can appear in a template: components, directives and pipes.
The standalone requirement
A dependency is deferrable only if it is standalone. A standalone dependency can be loaded on its own, because it declares everything it needs in its own imports array. A component declared in an NgModule cannot be loaded this way. Its compilation scope comes from the module, so the compiler cannot split it out safely.
| Dependency kind | Deferrable? | Reason |
|---|---|---|
| Standalone component, directive or pipe | Yes | It carries its own imports |
| Component declared in an `NgModule` | No | Its scope comes from the module |
| An `NgModule` in the `imports` array | No | Modules are not split by `@defer` |
| Standalone component used only in `@defer` | Yes | The only reference is deferrable |
| Standalone component also used outside `@defer` | No | An eager reference keeps it in the main bundle |
In Angular 19 and later, components are standalone by default, so a component you generate with the CLI usually qualifies. In older projects, check for standalone: true in the decorator.
You can use a module-based component inside a @defer block. The block still renders it. It just stays in the main bundle, and the block does not make it lazy.
Eager references
The more common problem is a standalone dependency that stays eager because something else references it. The rule is that every reference to the dependency in that file must be inside a @defer block. One reference elsewhere keeps the static import, and no lazy chunk is created for it.
There are three common ways to break the rule.
Use outside the defer block
<app-chart />
@defer (on viewport) {
<app-chart />
}The first <app-chart /> is eager. The component must be in the main bundle to render at once, so the deferred use gains nothing. If the first use is intended, the @defer is pointless. If it is a mistake, remove it.
Use in the component class
import { Component, viewChild } from '@angular/core';
import { ChartComponent } from './chart.component';
@Component({
selector: 'app-report',
imports: [ChartComponent],
template: `
@defer (on viewport) {
<app-chart />
}
`,
})
export class ReportComponent {
// This value reference keeps ChartComponent in the main bundle.
chart = viewChild(ChartComponent);
}A query such as viewChild(ChartComponent) is a runtime use of the class. The static import must stay, so the chunk is never created. The same holds for any other use of the class as a value in that file: a provider, a new call, or a comparison with instanceof.
Use in a sub-block
The @placeholder and @loading blocks render before the deferred code arrives. The compiler therefore treats dependencies used in them as eager. Treat @error the same way and keep every sub-block light. A placeholder that uses a heavy component defeats the purpose of the defer:
@defer (on viewport) {
<app-comments />
} @placeholder {
<!-- Keep this to plain markup or a small, already-loaded component. -->
<div class="comments-placeholder"></div>
}If <app-comments /> also appeared in the placeholder, it would be eager.
How the compiler decides
The decision is made per dependency, per component file.
Two details follow from this flow.
First, the check is per file. If ChartComponent is deferred in ReportComponent but another component, such as DashboardComponent, imports it normally and renders it outside a @defer, the class lands in the main bundle through DashboardComponent. The deferral in ReportComponent is still correct. It just does not remove the code from the initial download, because the bundler includes the module once for the whole application.
Second, transitive dependencies follow the deferred component. If ChartComponent imports a charting library and nothing else eager imports that library, the library goes into the lazy chunk with it. This is where most of the size savings come from. A small wrapper component often pulls in a large library.
Using one dependency in several blocks
A dependency can appear in more than one @defer block in the same template. The compiler still treats it as deferrable, because every use is inside a main defer block. The chunk is downloaded once. The triggers of each block decide when that block renders. Once the chunk has loaded, other blocks that use it do not download it again.
@defer (on interaction(showA)) {
<app-chart [data]="a" />
}
@defer (on interaction(showB)) {
<app-chart [data]="b" />
}The first block that loads brings in the chunk. The second one finds it already available.
Verifying that a chunk exists
Do not assume the split worked. Run a production build and read the output:
ng buildThe build report lists initial chunks and lazy chunks separately. A deferred dependency shows up as a lazy chunk with a generated name. Before the change, the same code was part of main-*.js. Compare the initial total before and after:
- Build once with the
@deferremoved, or with a known eager reference, and note the initial total. - Restore the
@deferand build again. - Check that the initial total dropped and a new lazy chunk appeared.
You can also open the browser's Network panel and reload. Scroll to the deferred area (for on viewport), and the lazy chunk should be requested at that moment, not at page load.
A worked fix
Here is a component that was meant to defer a chart but did not. The viewChild query kept the chart in the main bundle.
import { Component, viewChild } from '@angular/core';
import { ChartComponent } from './chart.component';
@Component({
selector: 'app-report',
imports: [ChartComponent],
template: `
@defer (on viewport) {
<app-chart #chart />
} @placeholder {
<div class="chart-placeholder"></div>
}
`,
})
export class ReportComponent {
chart = viewChild(ChartComponent);
}One fix is to drop the query. If the parent needs to talk to the chart, pass data in through inputs and receive events through outputs. Both work through the template, so no class reference is needed:
import { Component, signal } from '@angular/core';
import { ChartComponent } from './chart.component';
@Component({
selector: 'app-report',
imports: [ChartComponent],
template: `
@defer (on viewport) {
<app-chart [data]="points()" (selected)="onSelected($event)" />
} @placeholder {
<div class="chart-placeholder"></div>
}
`,
})
export class ReportComponent {
points = signal<number[]>([3, 5, 8]);
onSelected(index: number): void {
console.log('selected point', index);
}
}Now ChartComponent appears only inside the main defer block. The compiler can move it into a lazy chunk. Here ChartComponent is assumed to declare a data input and a selected output.
Edge cases to remember
- Template-only use is enough. You need no special syntax beyond
@defer. Theimportsarray entry stays. - A deferred block can contain other deferred blocks. Each nested block has its own triggers and its own dependency analysis.
- Directives and pipes follow the same rules. A pipe used both inside and outside a block is eager. A pipe used only inside the block is deferred.
- Unit tests are unaffected by the split. The component still imports its dependencies in the decorator. The test chapter later in this tutorial shows how to drive the block.
Exercise
A component imports ChartComponent in its imports array and uses <app-chart /> only inside a @defer block. The class also has the field chart = viewChild(ChartComponent);. The build succeeds, but no lazy chunk appears for the chart. Why, and how can you get the chunk?
Show answer
The viewChild(ChartComponent) query uses the class as a value in the same file. That is an eager reference, so the compiler keeps the static import and does not move the component into a lazy chunk. Remove the query, and pass data through inputs and outputs in the template. After that, the only reference is inside the @defer block, and the production build shows a separate lazy chunk.
Exercise
Which of these dependencies can the compiler defer when it is used only inside a @defer block: a standalone pipe, a component declared in an NgModule, a standalone directive? Name the one that cannot be deferred and explain the reason.
Show answer
The standalone pipe and the standalone directive can be deferred. The component declared in an NgModule cannot, because its compilation scope comes from the module, so it cannot be loaded as an independent unit. It still renders inside the block, but it stays in the main bundle.