AI Hints
Home›Angular Tutorial›Building for Production, @defer and Deploying
Angular · Lesson 110 of 120

@defer

Load parts later.

⏱ 2 min read▶ Video at 2:11:59🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Some parts of a page aren't needed straight away. Like reviews, at the bottom of a book page. At defer loads them later, only when they're needed.

  • Not needed straight away
  • Reviews, comments
  • Load them later

The @defer block

At defer wraps part of a template. Its components go into a separate chunk. A trigger says when to load it, like on interaction, or on viewport. Placeholder shows before, and loading shows while it downloads.

keyvalue
@defer (on interaction(btn))load on a click
on viewport / on idleother triggers
@placeholderbefore loading
@loadingwhile loading

Reviews on demand

The reviews component is inside at defer. It loads on interaction with the button. Until then, the placeholder explains what will happen. We click show reviews. Run it. The reviews chunk was downloaded, and the reviews appeared.

app.tstypescript
import { Component } from '@angular/core';
import { Reviews } from './reviews';

@Component({
  selector: 'app-root',
  imports: [Reviews],
  template: `
    <h2>Dune</h2>
    <button #more>Show reviews</button>
    @defer (on interaction(more)) {
      <app-reviews />
    } @placeholder {
      <p class="muted">Reviews load when you click.</p>
    } @loading {
      <p>Loading reviews...</p>
    }
  `,
})
export class App {}
Also uses reviews.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-reviews',
  template: `
    <div class="card">★★★★★ "A masterpiece." · Sam</div>
    <div class="card">★★★★☆ "Long, but worth it." · Alex</div>
  `,
})
export class Reviews {}
Also uses actions.json
[{"click": "button"}, {"wait": 600}]

A deferred chunk

Let's build the same app. Run it. Reviews has its own lazy chunk. It's only downloaded when someone asks for the reviews.

app.tstypescript
import { Component } from '@angular/core';
import { Reviews } from './reviews';

@Component({
  selector: 'app-root',
  imports: [Reviews],
  template: `
    <h2>Dune</h2>
    <button #more>Show reviews</button>
    @defer (on interaction(more)) {
      <app-reviews />
    } @placeholder {
      <p class="muted">Reviews load when you click.</p>
    } @loading {
      <p>Loading reviews...</p>
    }
  `,
})
export class App {}
Output
Initial chunk files | Names | Raw size | Estimated transfer size
chunk-G7HIYP5G.js | - | 114.25 kB | 34.29 kB
main-TZHUS65B.js | main | 111.28 kB | 28.50 kB
styles-RFJL2J33.css | styles | 790 bytes | 790 bytes
| Initial total | 226.31 kB | 63.58 kB
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-BLOB25MD.js | reviews | 456 bytes | 456 bytes
Application bundle generation complete. [0.680 seconds]
Also uses reviews.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-reviews',
  template: `
    <div class="card">★★★★★ "A masterpiece." · Sam</div>
    <div class="card">★★★★☆ "Long, but worth it." · Alex</div>
  `,
})
export class Reviews {}
Also uses _mode
prod

Summary

At defer splits part of a page into its own chunk, and loads it when a trigger fires. Next, deploying the app.

Key takeaways
  • @defer
  • on interaction
  • @placeholder
Next up: Deploying — put it online