
@defer Doesn't Always Shrink Your Bundle: What Really Gets Deferred
- كاتب موثّق
- Angular 22
محتويات المقال
@defer (on viewport). In the browser it behaves exactly as hoped: a gray skeleton, then the chart once you scroll down. But the next ng build reports the same Initial Total as before, and no new lazy chunk appears in the output. The cause is one line in the dashboard class, added weeks earlier so a toolbar button could export the chart as an image: viewChild(SalesChart).
The common misconception
@defer moves its code out of the main bundle. The block is the instruction, and the bundle shrinks.
What actually happens
The compiler splits per dependency, and only when it's allowed to
@defer block, Angular's compiler emits a dynamic import() instead of a static one. That's the whole mechanism behind here, and it only works if nothing else in the file needs the class right away. So the guide sets two conditions. First, a dependency must be standalone; one declared in an NgModule is loaded eagerly even inside @defer. That applies to the components you use in the block directly: what they use in turn may still come from NgModules and travels with them into the lazy chunk. Second, a dependency can't be referenced outside the @defer blocks of the same file; a reference elsewhere in the template, in the class, or in a viewChild() query makes it eager. Listing it in the component's imports array is fine; that's how the compiler finds it.
import { Component, viewChild } from '@angular/core';@Component({ selector: 'app-dashboard', imports: [SalesChart], template: ` <button (click)="chart()?.exportPng()">Export</button> @defer (on viewport) { <app-sales-chart /> } @placeholder { <div class="chart-skeleton"></div> } `,})export class Dashboard { // A reference outside the block: SalesChart becomes eager readonly chart = viewChild(SalesChart);}import { Component } from '@angular/core';@Component({ selector: 'app-dashboard', imports: [SalesChart], template: ` @defer (on viewport) { <app-sales-chart #chart /> <button (click)="chart.exportPng()">Export</button> } @placeholder { <div class="chart-skeleton"></div> } `,})export class Dashboard { // No reference to SalesChart outside the block: it's split out}#chart points at the instance after it loads, and the Export button now appears with the chart, which is the only time it could work anyway. SalesChart itself is imported straight from sales-chart.ts.
Only the main block moves
@placeholder, @loading and @error render before the deferred code arrives, so their own dependencies are loaded eagerly. A placeholder that uses a rich skeleton component, or a loading block with an animation library, adds that code to the initial bundle. Keep those blocks to plain HTML and light components.
on idle, which loads the chunk as soon as the browser is idle after the first render. That keeps it off the initial load, but every visitor still downloads the code. When most users never need the content, a trigger like on interaction or on viewport saves the download as well.
The bundler has the last word
SalesChart statically, the bundler has to put it in the initial chunks, whatever the dashboard template says. The guide calls out the most common case: importing through a like index.ts, which the bundler treats as one module, so the component stays in the main bundle regardless of @defer. An eager route has the same effect. The routing guide notes that a component named in a route's component property is bundled with the route configuration; that's what loadComponent is for.
import() | ||
viewChild() or outside the block | ||
@placeholder, @loading or @error | ||
How to apply it in your project
Measure first Build in production mode and note the Initial Total and the lazy chunk list. Without that number, you can't tell whether a @deferblock changed anything.Search each deferred class name In the file that holds the @deferblock, find every use of the deferred component's class besides itsimportsentry:viewChild(),contentChild(), template uses outside the block, type references. Move them inside the block or into the deferred component.Follow the imports outward Import deferred components from their own files, not from a barrel, and check that no eager route, shared component or NgModule imports them too. Keep the fallback blocks light Review what @placeholder,@loadingand@erroruse, and pick a trigger that matches how often users actually need the content.
Key takeaways
@deferonly splits out dependencies the compiler can load with a dynamicimport(); the block works either way.A deferred dependency must be standalone and unreferenced outside the block in the same file, viewChild()included.Placeholder, loading and error blocks load eagerly, and any eager import elsewhere keeps the code in the initial chunks. Verify with ng buildand aninitialbudget, not with the dev server's Network panel.
التعليقات (0)