Sub topic 2
Your first @defer block
This chapter assumes you know standalone components and the imports array, Angular's @if and @for control flow, and what a JavaScript bundle is. You should also have built an Angular app with the CLI at least once.
What a @defer block does
A @defer block wraps part of a template. The compiler moves the components, directives, and pipes used only inside that block into a separate JavaScript file, called a chunk. The browser downloads the chunk later instead of with the main bundle, so the initial download gets smaller.
Here is the smallest possible block:
@defer {
<app-comments />
}With no trigger, Angular uses on idle. It waits until the browser reports it has no urgent work, then downloads the chunk for app-comments and renders it. Until then, the block renders nothing.
Using it in a component
The component class does not change. CommentsComponent stays in the imports array, like any standalone dependency.
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 imports entry looks like an ordinary eager import. The compiler sees that CommentsComponent appears only inside the @defer block, so it builds that component into a lazy chunk. You don't write a dynamic import() yourself.
This example uses on viewport, which starts loading when the placeholder scrolls into view. It also adds a @placeholder block. Viewport triggers need a placeholder, because Angular watches the placeholder's element to know when it is visible. Later chapters cover placeholders and triggers in detail.
How the pieces fit together
The diagram shows what happens at runtime.
Confirming it loads separately
You can check the result without special tooling.
- Run
ng build. In the output, look for an extra lazy chunk file whose name relates to your component, listed apart frommainand the other initial files. - Run
ng serve, open the browser's Network tab, and filter by JS. - Reload the page. The chunk should appear as a separate request after the initial files, at the moment the trigger fires.
If you see no separate chunk, the dependency is probably still used somewhere eagerly. The next chapter covers that case.
Things to watch for
- The default trigger is
on idle. Writing@defer { ... }is the same as@defer (on idle) { ... }. - Inputs work as usual. You can bind inputs and outputs on a deferred component, such as
<app-comments [postId]="postId" />. The bindings are evaluated when the block renders, not before. - Only standalone dependencies can be deferred. Components declared in an NgModule cannot be split out this way.
- Content starts empty. Without a
@placeholder, the area is empty until the content arrives. That can shift the layout when the content appears, so most real blocks add a placeholder.
Exercise
A component lists ChartComponent in its imports array and uses <app-chart /> only inside @defer { ... }. Which trigger starts loading, and why does the chart's code not appear in the main bundle?
Show answer
With no trigger specified, the default on idle applies, so loading starts once the browser is idle. The chart's code is missing from the main bundle because the compiler sees it used only inside the @defer block and moves it into a separate lazy chunk. This works because ChartComponent is standalone.