Skip to content
ToolShedby Wasim Shaikh

Angular @defer Block Builder

Build Angular @defer blocks visually: on viewport, interaction, hover, idle, timer and when triggers, prefetch, incremental hydration triggers, and @placeholder, @loading and @error, with a step-by-step view of what loads when.

Presets:
Load triggers

When to load and show the block. Several triggers are OR-ed.

Prefetch triggers (optional)

When to download the code early, without showing it yet.

Hydrate triggers (SSR only, optional)

With incremental hydration, the server renders the content and these decide when it becomes interactive.

Sub-blocks

Template
@defer (on viewport; prefetch on idle) {
  <app-comments [postId]="postId()" />
} @placeholder {
  <p>Comments will load when you scroll here.</p>
} @loading (after 100ms; minimum 500ms) {
  <app-spinner />
} @error {
  <p>Comments failed to load.</p>
}

What happens, step by step

  1. With SSR or prerendering and no hydrate trigger, the server renders the @placeholder (or nothing), and the triggers start on the client.
  2. At first, Angular shows the @placeholder content.
  3. The JavaScript for the block starts downloading (without showing anything new) when the browser is idle.
  4. The block loads and renders when the placeholder scrolls into view, usually instantly because the code was prefetched.
  5. While the code downloads, the @loading content replaces the placeholder if loading takes longer than 100ms, and stays for at least 500ms once shown.
  6. If the download fails, for example offline or after a new deploy removed the old chunk, the @error content is shown.
  7. Only standalone components, directives and pipes inside the block are split into a lazy chunk, and only if they are not used elsewhere in the same template or in a @ViewChild query. Placeholder, loading and error content is loaded eagerly.

Import the deferred standalone components in the component's imports array as usual. The compiler turns them into lazy chunks.

What Is an Angular @defer Block?

A @defer block lazy-loads part of a template. The compiler moves the standalone components, directives and pipes inside it into a separate JavaScript chunk, and Angular only downloads and renders that chunk when a trigger fires: the block scrolls into view, the user clicks, the browser goes idle, or a condition becomes true. It is the simplest way to cut the initial bundle and speed up Largest Contentful Paint without touching the router. Pick the triggers and sub-blocks above to get the template and a plain-English timeline of what loads when.

@defer Trigger Cheat Sheet

SyntaxLoads when
on idleWhen the browser is idle (requestIdleCallback). The default. Optional timeout: on idle(500).
on viewportWhen the placeholder, or a referenced element, enters the viewport. Accepts IntersectionObserver options: on viewport({trigger: ref, rootMargin: '200px'}).
on interactionOn click or keydown on the placeholder or a referenced element.
on hoverOn mouseover or focusin on the placeholder or a referenced element.
on immediateAs soon as the non-deferred content has rendered.
on timer(2s)After a fixed duration in ms or s.
when exprWhen an expression becomes truthy. One-way: it never goes back to the placeholder.
prefetch on … / prefetch when …Download the code early with any of the triggers above, without rendering it yet.
hydrate on … / hydrate when …SSR only. Server-render the content and hydrate it when the trigger fires. Hydrate triggers take no template reference.
hydrate neverSSR only. Keep the server-rendered HTML static on the first load. Cannot be combined with other hydrate triggers.

Separate several triggers with a semicolon. Triggers of the same kind are OR-ed: @defer (on viewport; on timer(5s)) loads on whichever comes first.

Frequently Asked Questions

What is the default @defer trigger?
on idle. A @defer block with no trigger loads when the browser becomes idle, based on requestIdleCallback.
Why is my deferred component not in a separate chunk?
Only standalone components, directives and pipes are deferred, and only when they are not referenced outside the @defer block in the same template or in a @ViewChild query. Importing a component through a barrel file that is also imported eagerly elsewhere can pull it into the main bundle too. Content in @placeholder, @loading and @error is always loaded eagerly.
Why does on viewport or on interaction need a placeholder?
Without a template reference, these triggers watch the @placeholder. It must exist and have exactly one root element, so Angular has a single element to observe. Alternatively pass a reference to another element in the same template, such as on viewport(comments).
What does prefetch do?
It downloads the JavaScript for the block when its own trigger fires, without showing the content. Pair a late trigger with an earlier prefetch, such as on interaction; prefetch on hover, so the content appears instantly when the user clicks.
How do @loading after and minimum work?
after waits that long before showing the loading content, so a fast load never flashes it. minimum keeps it on screen for at least that long once it appears. Both timers start when loading is triggered. @placeholder only accepts minimum.
What happens to @defer blocks during SSR?
Without hydrate triggers the server renders the @placeholder (or nothing) and the triggers run on the client. With a hydrate trigger the server renders the main content, and the browser keeps it as static HTML until the hydrate trigger fires, then loads the code and hydrates it. Events that happen before hydration are replayed. In current Angular, incremental hydration is on by default with provideClientHydration().
Do I still need a @placeholder with hydrate triggers?
Yes. Hydrate triggers only apply to the first, server-rendered load. When users reach the page through client-side navigation, the block behaves like a normal @defer block and shows the placeholder.
How do I test a @defer block?
Configure TestBed with deferBlockBehavior: DeferBlockBehavior.Manual, get the blocks with fixture.getDeferBlocks(), and call render(DeferBlockState.Loading) or render(DeferBlockState.Complete) to step through the states.

Sources & Official Resources

Syntax and behaviour follow the Angular documentation. Last reviewed , and every preset was parsed with the Angular 22.2 template compiler.

Lazy-loading whole routes instead? Use the Route Config Builder. Still on *ngIf? The Control Flow Converter moves templates to @if and @for. More in Angular tools.