October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

Deferrable Views in Angular: How @defer Controls When Code Loads

Angular's @defer block delays loading eligible template dependencies until a trigger or condition fires. Here is how eligibility, triggers, prefetching, SSR, and accessibility work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A deferrable view is an Angular @defer block. It holds template content whose dependencies (components, directives, and pipes) are loaded separately from the main application code, and only when a trigger fires or a condition becomes true. When the deferred content is not needed to paint the first screen, this can reduce the initial bundle. It is a loading and rendering choice, not a guaranteed performance gain, and it comes with eligibility rules, layout and accessibility trade-offs, and server-rendering behavior that you need to plan for before adopting it.

What a deferrable view does

Without @defer, every component used in a template is part of the bundle that the browser downloads before the page can run. Wrapping part of a template in a @defer block tells the Angular compiler to split the eligible dependencies out of that bundle. The compiler creates dynamic imports for them, along with their component CSS, and the block renders once those imports resolve. Angular does not guarantee the order in which the deferred imports resolve, so do not write code that depends on one component being ready before another. (Angular’s deferred loading guide)

As an Amazon Associate I earn from qualifying purchases.

Which components can be deferred

Eligibility is the rule most often missed. A dependency is only split out if it meets all of these conditions:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • It is a standalone component, directive, or pipe. Non-standalone declarations stay in the main bundle.
  • It is not referenced anywhere else in the same file outside the @defer block.
  • It is not targeted by a ViewChild query, because that query would pull it into eager code.

Transitive dependencies, meaning the components that a deferred component itself uses, do not all need to be standalone. If an eligibility condition fails, Angular still renders the content, but the dependency is not deferred, so the expected saving does not happen. Check the bundle output rather than assuming a block is split. (Angular’s deferred loading guide)

Basic syntax

A complete block can contain a main body and up to three optional state blocks:

@defer {
  <large-component />
} @placeholder {
  <p>Content will load when needed.</p>
} @loading (after 100ms; minimum 1s) {
  <p>Loading…</p>
} @error {
  <p>Could not load this content.</p>
}

The main body is the content whose dependencies are deferred. The @placeholder block is shown before the trigger fires, @loading is shown while the dependencies download, and @error is shown if the download fails. Only the main body is deferred. The three state blocks are loaded eagerly, so any components inside them are not deferred. Keep them light. (Angular’s @defer API reference)

Controlling when the content loads

A block with no trigger loads when the browser becomes idle. You can change that with an on trigger, a when condition, or both. The table lists the documented triggers and the situations they suit.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Trigger Syntax Loads when Suits
Idle (default) @defer or @defer (on idle) The browser is idle Content that is useful but not urgent
Viewport @defer (on viewport) The placeholder enters the viewport Content lower on the page, such as a comments section
Interaction @defer (on interaction) The user interacts with the placeholder Panels or dialogs the user opens on request
Hover @defer (on hover) A pointer hovers over the placeholder Pointer-oriented previews
Immediate @defer (on immediate) Right after non-deferred content has rendered Content that should load soon but not block the first render
Timer @defer (on timer(500ms)) After the specified delay A fixed, predictable delay
Custom condition @defer (when isReady) The expression becomes true App-specific readiness, such as a feature flag or a loaded data set

Idle, the default

Idle loading suits content that should eventually appear without competing with the work that makes the page usable. It is the right starting point when you are unsure which trigger fits.

Viewport, interaction, and hover

These triggers respond to what the user does or sees. Use viewport for content approaching the visible area, interaction for panels the user explicitly opens, and hover for previews that a pointer device reveals. Hover is not a good fit for touch-only audiences, because the trigger depends on pointer input.

Immediate and timer

immediate loads the deferred content after the non-deferred content renders, which is useful when you want the load to start quickly without delaying the first paint. timer waits a fixed time. Both are predictable, but neither reacts to what the user is doing.

Custom conditions with when

A when expression lets you load content based on app state. Once the condition has caused the block to load, the block stays rendered. It does not revert to the placeholder if the expression later becomes false. Plan for that if your condition can change back.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Multiple triggers act as OR conditions

If a block lists more than one trigger, the block loads when any one of them fires. For example, on viewport; on interaction loads the content on whichever happens first.

Prefetching is separate from rendering

A prefetch condition controls when the dependencies are fetched. It does not control when the block is displayed. Use it to start downloading code early so that the visible trigger finds the code ready. For example, this block fetches the code during idle time but only renders when the user interacts with it:

@defer (on interaction; prefetch on idle) {
  <comment-thread />
} @placeholder {
  <button>Show comments</button>
}

The prefetch option accepts on triggers and when conditions, just like the main block. Prefetching adds network requests earlier, so it is worth using only when you expect the user to need the content soon. (Angular’s deferred loading guide)

Placeholder, loading, and error states

The state blocks shape what the user sees while the deferred code is pending or unavailable. Two timing options on @loading help avoid flicker. after delays showing the loading state, so fast loads never display it. minimum keeps it visible for at least a set duration, so a quick load does not produce a brief flash. In the example above, after 100ms waits 100 milliseconds before showing the loading content, and minimum 1s keeps it visible for at least one second once shown.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Give the @placeholder a size close to the final content. A placeholder that is much smaller than the loaded component causes the page to jump when the content arrives. This matters most for content in the initial viewport, discussed below.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Nested defer blocks

A defer block can contain another defer block. If the inner and outer blocks use identical triggers, they can fire at the same moment and produce a cascade of simultaneous requests. Angular’s guidance is to use different triggers for nested blocks so the requests are spread out. A common pattern is an outer viewport trigger with an inner interaction trigger, so the inner content loads only after the outer content has appeared and the user has acted.

Server rendering and hydration

By default, server-side rendering (SSR) and static site generation (SSG) render the placeholder, or nothing if there is no placeholder. Defer triggers do not run on the server, so the deferred content is not in the server-rendered HTML. If that content matters for search indexing or first paint, you need a different approach.

Incremental Hydration changes this. With it, hydrate triggers can load the dependencies during server rendering and render the main template, and then hydrate the block according to the configured trigger. The Angular Incremental Hydration guide describes the setup and the trigger options. (Angular’s Incremental Hydration guide)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Layout shift and accessibility

Deferring content that is visible in the initial viewport is the most common layout mistake. When the placeholder is replaced by the loaded component, the surrounding content moves, which can increase cumulative layout shift (CLS). Angular advises against deferring that content. Reserve the space with a placeholder of matching size, or leave initial-viewport content eager.

Screen readers have a related problem. A user may encounter only the placeholder or loading text, and the eventual change may go unannounced. Angular’s guide shows wrapping the block in a live region so that state changes are announced:

<div aria-live='polite'>
  @defer (on viewport) {
    <comment-thread />
  } @placeholder {
    <p>Comments are loading when you scroll here.</p>
  } @loading {
    <p>Loading comments…</p>
  }
</div>

Test the result with a screen reader on the real page, because a live region announces only the text that changes inside it.

Before you adopt deferrable views

  • Confirm each deferred dependency is standalone and is not referenced eagerly or through a ViewChild query.
  • Check the build output to see whether the expected chunks were actually created.
  • Do not defer content that is visible in the initial viewport unless the placeholder reserves the same space.
  • Use a different trigger for each level of nested defer blocks.
  • Use prefetch only where an early network request is justified.
  • Keep @placeholder, @loading, and @error content small, since those components are loaded eagerly.
  • If you use SSR or SSG, decide whether deferred content must appear in the server HTML, and if so, evaluate Incremental Hydration.
  • Wrap state changes in a live region and test with a screen reader.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.