Sub topic 3
What can be deferred: standalone dependencies and eager references
This chapter assumes you can write standalone components with an imports array and that you have written a basic @defer block. It covers which dependencies Angular can move into a lazy chunk, and what silently stops it from doing so.
How the compiler decides
You do not tell Angular which classes to split out. The compiler looks at the template and collects every component, directive, and pipe used inside the @defer block. It then checks whether each one is safe to move to a separate chunk. A dependency is deferred only if it meets both rules:
- It is standalone.
- It is not referenced outside
@deferblocks in the same file.
If a dependency fails either rule, the build still succeeds and the block still works. The code just stays in the main bundle, so you gain nothing.
Rule 1: the dependency must be standalone
A standalone component, directive, or pipe declares its own dependencies, so the compiler can package it with everything it needs. A component declared in an NgModule cannot be extracted this way, because it depends on the module's compilation scope.
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 {}CommentsComponent stays in the imports array. You do not replace it with a dynamic import(). The compiler rewrites the generated code so the class is loaded from a separate chunk when the block runs.
The chunk also contains the deferred component's own standalone dependencies, unless the main bundle already needs them. Deferring one component can therefore move a whole subtree of code.
Rule 2: no eager references in the same file
The static import statement at the top of the file is what pulls a class into the main bundle. If anything outside a @defer block uses the class, the compiler must keep that import, and the deferral is lost.
Common eager references:
- Using the same component in the template outside any
@deferblock. - Naming the class in the component's TypeScript, for example in a
viewChild(...)query or a type annotation that is used at runtime.
import { Component, viewChild } from '@angular/core';
import { ChartComponent } from './chart.component';
@Component({
selector: 'app-report',
imports: [ChartComponent],
template: `
@defer (on viewport) {
<app-chart />
} @placeholder {
<div class="chart-placeholder"></div>
}
`,
})
export class ReportComponent {
// Eager reference: ChartComponent stays in the main bundle.
chart = viewChild(ChartComponent);
}This code compiles and runs, but ChartComponent is bundled with ReportComponent. To fix it, remove the class reference. If the parent needs to talk to the chart, expose an output() from ChartComponent or share state through a service.
Confirming the result
Run a production build and read the list of generated chunks. A deferred dependency shows up as its own lazy chunk file, and the initial bundle size drops by roughly that amount. If you expected a chunk and see none, look for an eager reference in the component file first, then check that the dependency is standalone.
Mixing deferred and eager usage on purpose
Sometimes you want a component available immediately in one place and deferred in another. This is allowed, but the eager use wins: the class goes into the main bundle, and the deferred block simply finds it already loaded. Use this only when the eager use is real, not by accident.
Exercise
A component imports ChartComponent in its imports array and uses <app-chart /> inside a @defer block. The same template also renders <app-chart /> once above the block, outside any @defer. The build shows no lazy chunk for the chart. Why, and how do you get the chunk?
Show answer
The use outside the @defer block is an eager reference, so the compiler keeps the static import and ChartComponent stays in the main bundle. Remove the eager usage, or move all usages inside @defer blocks, so the only references to the class are deferred ones. The same applies to a viewChild(ChartComponent) query in the class. The component must also be standalone.