Sub topic 1
Why defer part of a template: bundles and lazy loading
Every component a page uses ships as JavaScript. The more of it the browser must download, parse and run before the page works, the slower the first view. This chapter explains how Angular's @defer block moves part of a template out of that initial download.
Prerequisites
This chapter assumes you already know:
- Angular standalone components and the
importsarray. - Template syntax and built-in control flow such as
@ifand@for. - What a JavaScript bundle is and the basic idea of lazy loading.
- How to run an Angular CLI build and read its output.
The problem: one large initial bundle
When you build an Angular application, the build tool collects your code into files called bundles. The browser must download the initial bundle before it can show anything. A component that is imported by another component is included in that bundle, even if the user never sees it. Examples are a comments section far down the page, a chart behind a tab, or a premium-only panel.
Code that is downloaded but not needed yet makes the first load slower. Smaller initial bundles usually mean faster startup.
Code splitting and lazy loading
Code splitting is the build step that divides an application into several files, called chunks. Lazy loading is the runtime behavior of fetching a chunk only when it is needed.
You may already use this with the router. A route configured with loadComponent puts that page in its own chunk, which loads when the user navigates there. The router splits code at page boundaries. @defer splits code inside a single template.
What @defer does
The smallest deferrable view looks like this:
@defer {
<app-comments />
}The Angular compiler sees that app-comments is used only inside the @defer block. It then removes the component from the main bundle and places it in a separate chunk. At runtime, Angular downloads that chunk and renders the block's content. With no trigger specified, the default is to start loading when the browser is idle.
This only works when the component is a standalone component listed in the imports array and is not used anywhere else outside a @defer block:
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 {}You still write a normal import and a normal imports entry. You do not write import() calls yourself. The compiler does the transformation.
Checking that it worked
Run a production build and read the list of output files:
ng buildAfter you add @defer, the output lists an extra lazy chunk file. The initial total size should drop by roughly the size of the deferred component and its own dependencies. If no new chunk appears, the component is probably referenced somewhere eagerly. A later chapter covers that case.
When splitting is worth it
Deferring is a trade-off. You save initial download size, but the deferred content appears later, and the user may see a gap while it loads.
- Good candidates are content below the fold, heavy widgets such as charts or editors, and features only some users reach.
- Poor candidates are content visible at first paint, and small components, where the extra network request costs more than the bytes saved.
Later chapters show how placeholders, triggers and prefetching reduce that gap.
Exercise
A component is listed in a standalone component's imports array and used only inside a @defer block. What does the Angular compiler do with it, and what do you change in your source code to get this?
Show answer
The compiler moves the component into a separate lazy chunk, so it is no longer in the initial bundle. You change nothing except wrapping the component's usage in @defer. The normal import and imports entry stay as they are.