الانتقال إلى المحتوى
FrontendAngular· 6 دقائق للقراءة

@defer Doesn't Always Shrink Your Bundle: What Really Gets Deferred

The block works either way. Whether its code leaves the initial bundle depends on every other reference to it.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A team wraps the dashboard's sales chart, the heaviest component on the page, in @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

The belief: putting a component inside @defer moves its code out of the main bundle. The block is the instruction, and the bundle shrinks.

It's what the first paragraph of the guide promises: deferrable views reduce the initial bundle size, and the code for the components, directives and pipes inside the block is split into a separate JavaScript file. The behavior on screen confirms it, since the placeholder renders first and the real content arrives later, exactly like a lazy-loaded block should. The template looks right and the page looks right. What neither of them shows is where the code ended up.

What actually happens

The compiler splits per dependency, and only when it's allowed to

For each component, directive and pipe used in the main @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.

When a condition fails, the block still works. Angular simply renders the placeholder, runs the trigger, and then shows content whose code was there all along.

TypeScript dashboard.ts
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);}

The fix moves the only outside reference into the block. The template reference variable #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.

Deferring also isn't skipping. The default trigger is 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

The compiler can only rewrite the file it's compiling. If anything that loads at startup imports 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.

What you deferSplit out?Why
Standalone component used only in the main blockYesThe compiler emits a dynamic import()
Same component also used by viewChild() or outside the blockNoAny outside reference in the file keeps it eager
A component declared in an NgModuleNoOnly standalone dependencies can be deferred
Anything in @placeholder, @loading or @errorNoThose blocks render first, so they load eagerly
A component imported via a barrel or by eager codeNoThe bundler keeps it in the initial chunks

How to apply it in your project

  1. Measure firstBuild in production mode and note the Initial Total and the lazy chunk list. Without that number, you can't tell whether a @defer block changed anything.
  2. Search each deferred class nameIn the file that holds the @defer block, find every use of the deferred component's class besides its imports entry: viewChild(), contentChild(), template uses outside the block, type references. Move them inside the block or into the deferred component.
  3. Follow the imports outwardImport deferred components from their own files, not from a barrel, and check that no eager route, shared component or NgModule imports them too.
  4. Keep the fallback blocks lightReview what @placeholder, @loading and @error use, and pick a trigger that matches how often users actually need the content.

Key takeaways

  • @defer only splits out dependencies the compiler can load with a dynamic import(); 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 build and an initial budget, not with the dev server's Network panel.

📚 المصادر

  1. Deferred loading with @defer — Angular
  2. Route loading strategies — Angular
  3. Building Angular apps (size budgets) — Angular

التعليقات (0)