Deferrable views: the @defer block

Intermediate · 28 min

Lazily rendering a chunk of template based on a trigger, with built-in placeholder, loading, and error states.

Before thisng-template, ng-container, and ngTemplateOutletLazy loading routes and components

procademy · 28 min

Deferrable views: the @defer block

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by procademyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • @defer splits off the content inside it (and the components it depends on) into a separate chunk, fetched only when its trigger condition is met — the same route-level lazy-loading idea, applied at the level of a single template block.
  • @defer's triggers decide when the deferred content starts loading — on viewport (scrolled into view), on interaction, on idle browser time, on a timer, or combinations of them.
  • `@placeholder`, `@loading`, and `@error` are the sub-blocks that render before, during, and if the deferred content fails to load — without them, there's simply nothing there until the content resolves.

Lazy loading, at the level of one template block

The lazy-loading lesson a few sections back split code off at the level of an entire route — a whole page’s worth of components, fetched only when a user navigates there. @defer applies the identical underlying idea at a much finer grain: a single chunk of template, inside an otherwise eagerly-rendered page, split off into its own bundle and fetched only when some condition is met. A heavy, below-the-fold widget — a comments section, a rich chart component — is the textbook case: the rest of the page renders immediately, and that one expensive piece’s code doesn’t even get downloaded until it’s actually needed.

@defer (on viewport) {
  <comments-widget />
} @placeholder {
  <div class="comments-skeleton"></div>
} @loading (minimum 200ms) {
  <spinner />
} @error {
  <p>Couldn't load comments.</p>
}

Triggers: deciding when “needed” actually happens

on viewport is one of several triggers — it fires once the @defer block scrolls into the visible viewport, which is exactly right for below-the-fold content a user might never even scroll to. Others cover different real scenarios: on interaction waits for a click or focus on a specified element (ideal for content behind a “show more” toggle), on idle waits for the browser to have spare time after more urgent work is done, on timer(2s) waits a fixed duration, and triggers can combine (on viewport; on timer(5s)) so whichever condition fires first wins. Choosing the right trigger is choosing the actual real-world signal that means “this content is about to matter to the user” — a bad trigger choice (say, deferring something that’s immediately visible with no scrolling at all) gains you nothing, since it’ll just trigger instantly anyway.

The sub-blocks: covering every state honestly

@placeholder is what renders before the trigger fires — without it, that space is simply empty, which can look like a layout bug rather than an intentional, not-yet-loaded state. @loading covers the gap between the trigger firing and the content actually finishing its fetch — the minimum 200ms option above is a deliberate detail: without a minimum, a loading indicator that appears and disappears in 50ms reads as a flicker rather than useful feedback, so @defer lets you enforce a floor on how long it stays visible once shown. @error covers the fetch genuinely failing — a network blip or chunk-loading failure — with its own fallback content instead of leaving the user looking at a loading spinner that silently never resolves.

prefetch: fetching early without rendering early

A trigger controls when deferred content renders — but you can separately control when its code is fetched, ahead of that, with a prefetch clause:

@defer (on interaction; prefetch on idle) {
  <comments-widget />
}

This tells Angular to render on interaction, as before, but to start downloading the widget’s code during idle browser time — well before the user actually clicks. By the time they do, the code is often already cached, and the perceived delay between clicking and content appearing shrinks close to zero. This separation — one condition for fetching, a different one for rendering — is worth noticing specifically because it’s easy to assume a single trigger has to do both jobs at once.

This lesson’s video builds a complete @defer example with all four sub-blocks and several trigger types, long enough to cover the full feature properly rather than a narrow slice of it; the linked “deeper” video is a second full pass if you want to see the same mechanics built differently.

Check yourself

  1. 01A heavy comments widget is wrapped in `@defer (on viewport) { <comments-widget /> }`. What determines when the browser actually fetches comments-widget's code?

    The moment the `@defer` block itself scrolls into the user's viewport — not page load, not component construction. If a user never scrolls far enough to see it, the comments widget's code is never fetched at all, the same lazy-loading benefit route-level `loadComponent` gives you, just triggered by scroll position instead of navigation.

  2. 02What actually renders in place of a @defer block before its trigger condition has been met, if no @placeholder sub-block is provided?

    Nothing — an empty space where the deferred content will eventually go. @placeholder exists specifically to give that gap real content (a skeleton loader, a static preview) instead of leaving a blank area until the trigger fires and the content resolves.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark