Sub topic 7
on idle, on immediate and on timer
This chapter covers the three triggers that start loading a deferred chunk without any user action. It assumes you know standalone components, the imports array, the @defer block with its @placeholder, @loading and @error sub-blocks, and how a trigger starts the download of a lazy chunk. You also need basic familiarity with the browser event loop.
What these three triggers have in common
A trigger is the condition that tells Angular to start loading the deferred chunk. The three triggers in this chapter depend only on the browser's schedule, not on what the user does.
on idlewaits until the browser has spare time.on immediatestarts as soon as the surrounding template has rendered.on timer(duration)waits a fixed delay.
None of them needs a reference to another element. That makes them different from on viewport, on interaction and on hover, which watch for something to happen in the page.
Each trigger fires at most once per block. After the chunk has loaded and the content has rendered, the block stays in its complete state. It does not unload or reload.
The diagram shows when each trigger fires, relative to the first render of the page.
on idle
on idle is the default trigger. These two blocks behave identically:
@defer {
<app-comments />
}
@defer (on idle) {
<app-comments />
}Angular waits until the browser reports an idle period, then starts loading the chunk. Internally it uses requestIdleCallback. In environments that lack that API, it falls back to a timeout. You do not need to write that fallback yourself.
"Idle" means the main thread has no urgent work. On a busy page, the trigger can fire later than you expect. On a page that finishes rendering quickly, it fires almost immediately after the first paint.
Use on idle for content that the user will probably want but does not need for the first paint. Examples are a comments section below the article, a recommendations list, or a chat widget.
Idle does not mean "never during startup"
A common misunderstanding is that idle loading cannot affect startup performance. It can, in two ways:
- If the page has little work to do, idle arrives right after the first render. The chunk then downloads while the user is still looking at the first screen, and it competes with images and other requests for bandwidth.
- The chunk, once downloaded, must be parsed and executed, and the component must be created. That work happens on the main thread and can delay input handling if the chunk is large.
If this matters, split a large deferred area into smaller blocks, or use on timer to push the work further out.
on immediate
on immediate starts loading as soon as Angular has finished rendering the non-deferred content of the template.
@defer (on immediate) {
<app-dashboard-widgets />
} @placeholder {
<p>Preparing widgets...</p>
}This trigger does not wait for idle time. The page still renders its eager content first, and then the chunk request starts.
Why use it, when the content could simply sit in the main bundle? There are two reasons:
- The content is needed right away, but you want the first render of the rest of the page to not wait for its code. The initial bundle stays smaller, and the deferred area appears a moment later.
- You want one trigger that reflects intent clearly. A reader of the template sees that this block is deferred for bundle size only, not for timing.
The cost is that the user sees the placeholder or loading state briefly, and a layout change when the content replaces it. If the content is above the fold and always shown, think twice. Eager code has no loading step, and it avoids the shift.
on timer
on timer starts loading after a delay. The duration accepts milliseconds with ms or seconds with s.
@defer (on timer(5s)) {
<app-newsletter-signup />
} @placeholder {
<div class="signup-slot"></div>
}@defer (on timer(750ms)) {
<app-related-articles />
}The timer starts when Angular evaluates the block, which is when the parent template renders. It does not start when the browser becomes idle, and it does not start when the placeholder appears.
Typical uses:
- Content that should arrive a short time after the main content, such as a promotional banner.
- Staggering several deferred blocks so they do not all download together. Give them different delays like
on timer(1s)andon timer(3s).
Choose delays you can justify. A fixed delay is a guess about the user's device and network. It does not adapt: on a slow connection the delay elapses and the download still takes a long time.
Combining triggers
Several triggers in one @defer are separated by semicolons. Loading starts when any one of them fires. This works as an OR.
@defer (on idle; on timer(10s)) {
<app-activity-feed />
} @placeholder {
<p>Activity loads shortly.</p>
}Combining on immediate with other triggers has no practical effect, because on immediate always wins. Combining on idle with on timer is mostly useful as an upper bound for a second trigger. A more common pattern pairs a timing trigger with a user trigger, as in @defer (on interaction(btn); on timer(30s)). There the content loads on click, or after 30 seconds if the user never clicks.
You can also mix triggers with a when condition, which later chapters cover in detail:
@defer (on timer(5s); when forceLoad()) {
<app-report />
}Prefetching with timing triggers
A prefetch trigger uses the same trigger names, but it only downloads the chunk. It does not render the content. The main trigger decides when rendering happens.
<button type="button" #openReport>Open report</button>
@defer (on interaction(openReport); prefetch on timer(3s)) {
<app-report />
} @placeholder {
<p>Press the button to open the report.</p>
}Here the chunk downloads three seconds after the template renders. When the user clicks, the code is usually already in the browser cache of the running page, so the content appears without waiting.
Prefetching with on immediate as the main trigger has no benefit. The block already loads and renders at once, so there is nothing to prepare.
Placeholders matter more with timing triggers
The @placeholder block is optional for these three triggers. Without one, nothing is shown in the block's place until the content renders. For on idle and on timer in particular, that gap can last seconds, and the content then appears and pushes other elements down.
Reserve the space. Give the placeholder the same approximate size as the final content, for example with a fixed height in CSS:
import { Component } from '@angular/core';
import { NewsletterSignupComponent } from './newsletter-signup.component';
@Component({
selector: 'app-footer-area',
imports: [NewsletterSignupComponent],
styles: [`
.signup-slot { min-height: 160px; }
`],
template: `
<footer>
<p>Thanks for reading.</p>
@defer (on timer(5s)) {
<app-newsletter-signup />
} @placeholder (minimum 300ms) {
<div class="signup-slot"></div>
}
</footer>
`,
})
export class FooterAreaComponent {}The minimum 300ms parameter keeps the placeholder visible for at least that long once shown. This prevents a flash if the chunk is already cached and renders very quickly. A placeholder rendered in the first pass is always shown at least briefly before the swap, so the parameter mostly matters when content swaps in quickly.
Choosing between the three
| Trigger | Starts loading | Best for | Main risk |
|---|---|---|---|
| `on idle` | When the browser is idle | Secondary content below or beside the main content | Fires early on quiet pages and competes for bandwidth |
| `on immediate` | After the eager template renders | Content needed at once, but not in the first bundle | Visible loading step and layout shift |
| `on timer(t)` | After a fixed delay | Low-priority content, staggered loading | The delay is a guess and ignores network speed |
When unsure, start with the default on idle. Measure with the browser's network panel and a Lighthouse run. Then change the trigger only where the numbers show a problem.
Common mistakes
- Expecting
on timerto wait for idle. The timer counts from when the block is evaluated. If the main thread is busy, the callback runs when it is free, but the delay itself does not include idle waiting. - Deferring a component that is also used eagerly. If the same component appears outside a
@deferblock in the same file, or is referenced in the component class, the compiler keeps it in the main bundle. The deferral then saves nothing. - Leaving out the placeholder. The content jumps into the page when it loads.
- Using very short timers as a substitute for
on immediate.on timer(1ms)adds a timeout for no benefit. Useon immediate. - Relying on these triggers during server-side rendering. On the server, the placeholder is rendered and the triggers do not run. The client takes over after hydration. Plan the placeholder as the server-rendered output.
Exercises
Exercise
A page shows a related-articles list at the bottom. You want its chunk downloaded soon after the page renders, but you want to avoid competing with images that load in the first second. Which trigger would you try first, and what is its weakness?
Show answer
Try on timer(2s) or a similar delay, or the default on idle. The timer pushes the download past the initial image requests. Its weakness is that the delay is fixed and ignores the device and network. On a slow connection the download still takes long, and on a fast one the user waits longer than needed.
Exercise
What does this block do, and when does the chunk start downloading? @defer (on interaction(openReport); prefetch on timer(3s)) { <app-report /> }
Show answer
The chunk starts downloading about three seconds after the template renders, because of the prefetch trigger. The content renders only when the user interacts with the element referenced as openReport. If the download has finished by then, the content appears without a wait. If the user clicks earlier, Angular starts or continues the load and renders the content once it arrives.
Exercise
Why is @defer (on immediate; on idle) the same as @defer (on immediate)?
Show answer
Triggers separated by semicolons work as an OR: the first one to fire starts loading. on immediate fires right after the eager template renders, before the browser is likely to report idle, so on idle adds nothing.