Sub topic 7
on idle, on immediate and on timer
This chapter covers the three triggers that start loading without any user action: on idle, on immediate and on timer. It assumes you can write a standalone component, you know what the imports array does, and you have seen a basic @defer block. It also assumes you know that @placeholder content is what the template shows before loading begins.
What these three triggers have in common
A trigger decides when Angular starts fetching the lazy chunk and then renders the deferred content. The triggers in this chapter depend only on the browser's state or on a clock. They do not depend on what the user does or on what is on screen.
| Trigger | When loading starts | Typical use |
|---|---|---|
| `on idle` | When the browser reports it is idle | Content below the fold that you want soon, but not at the cost of startup work |
| `on immediate` | Right after the template renders, without waiting | Content that is needed now but is not worth blocking the first render |
| `on timer(duration)` | After the given delay | Content that should appear after a known pause |
on idle
on idle is the default. These two blocks behave the same way:
@defer {
<app-comments />
}
@defer (on idle) {
<app-comments />
}Angular uses the browser's requestIdleCallback function to detect idle time. The callback runs when the main thread has nothing urgent to do, so the chunk download does not compete with rendering and input handling. On the server, or in a browser without that API, Angular falls back to a timer.
Use on idle for content the user will probably want but does not need immediately, such as a comments section, a related-articles list, or a footer widget.
on immediate
on immediate starts loading as soon as the template renders. It does not wait for idle time.
@defer (on immediate) {
<app-hero-banner />
} @placeholder {
<div class="hero-placeholder"></div>
}The block is still separate from the main bundle, so the initial JavaScript is smaller. The difference from a normal template is that the first render shows the placeholder, and the real component replaces it once the chunk arrives. Use it for content that matters right away but whose code you want out of the critical path.
If the content is visible on first paint, deferring it usually makes the page worse. The placeholder appears first and the content follows, which can look like a flash or a jump. The chapter on layout shift covers this.
on timer
on timer waits for a duration and then starts loading. The duration is a number followed by ms or s.
@defer (on timer(5s)) {
<app-newsletter-signup />
} @placeholder {
<div class="newsletter-placeholder"></div>
}The timer starts when the template renders. The block does not appear after exactly five seconds. The delay is followed by the time the chunk takes to download, so the content shows up a little later.
Use on timer for content that should not compete with the main task, such as a promotional banner or a signup prompt.
Combining triggers
You can list several triggers in one @defer, separated by semicolons. Loading starts when any of them fires.
@defer (on timer(10s); on idle) {
<app-recommendations />
}Here the first trigger to fire wins. Because idle time usually arrives within a few milliseconds of rendering, the timer adds nothing in this example. Combining triggers is more useful when they are different in kind, such as a timer alongside a user trigger. Remember that a combined list is an "or", never an "and".
How the triggers fit together in a component
This component shows all three in a single template. The imports array lists each component, and each one is used only inside a @defer block, so the compiler moves each into its own lazy chunk.
import { Component } from '@angular/core';
import { RelatedComponent } from './related.component';
import { HeroBannerComponent } from './hero-banner.component';
import { NewsletterComponent } from './newsletter.component';
@Component({
selector: 'app-article',
imports: [RelatedComponent, HeroBannerComponent, NewsletterComponent],
template: `
@defer (on immediate) {
<app-hero-banner />
} @placeholder {
<div class="hero-placeholder"></div>
}
<article>...</article>
@defer (on idle) {
<app-related />
}
@defer (on timer(8s)) {
<app-newsletter />
}
`,
})
export class ArticleComponent {}Common mistakes
- Expecting
on timerto be precise. The duration is a minimum wait before loading starts. Download time is added to it. - Using
on immediateon content already in the first viewport without a sized placeholder. The page shifts when the real content replaces the placeholder. - Leaving out
@placeholderwith a non-immediate trigger. Nothing renders until loading is triggered, so the area is empty and may change the page layout when the content arrives. - Importing the component somewhere else in the same file. If the component is referenced outside a
@deferblock, for example in another part of the template or in aviewChildquery, it is bundled eagerly and deferring it has no effect on bundle size.
Exercise
A page shows a newsletter signup form that should appear about 10 seconds after the page renders. The component's code must not be in the main bundle. Which @defer line do you write, and why might the form appear later than 10 seconds?
Show answer
Write @defer (on timer(10s)) { <app-newsletter /> }, with the component listed in imports and used only inside that block. The timer starts after the template renders and only then does Angular begin fetching the chunk, so the form appears after the 10 seconds plus the time the chunk takes to download and render.