Deferrable views

@defer lets a template render part of itself lazily. The deferred component and its dependencies are split into a separate chunk and fetched only when a trigger fires. Ship less JavaScript up front, hydrate heavy UI on demand, and keep the initial render fast for better Core Web Vitals. Every chart below is synthetic and deterministic.

Pick a trigger

@defer (on viewport)Renders when the block scrolls into view. Ideal for below-the-fold widgets.

linkedSignal · derived, but writable

A computed is read-only, which suits a value you only ever read. But a selection is both derived (it should default from its source) and writable (the user picks). linkedSignal is exactly that: it re-derives when its source changes, yet accepts a manual write that sticks until the next source change. Swap the dataset and the selection snaps back to the first region; pick a region and your choice holds.

linkedSignalRegion Pacific in North Americasource default

1 · @defer (on viewport)

The chart below renders only once it scrolls into view. Until then a skeleton (@placeholder) holds its space. Once triggered, @loading (minimum 300ms) shows while the chunk loads and stays up at least 300 ms, so a fast load doesn't flicker.

Scroll down inside this box to trigger the deferred chart ↓

View source

The page template holds all four triggers; this one is the on-viewport block. HeavyChart is referenced only inside it, so it builds into its own chunk and isn't requested until the placeholder scrolls into view.

html
<header class="page-head">
  <h1 class="page-head__title">Deferrable views</h1>
  <p class="page-head__lead">
    <code>&#64;defer</code> lets a template render part of itself lazily. The deferred component and
    its dependencies are split into a separate chunk and fetched only when a trigger fires. Ship
    less JavaScript up front, hydrate heavy UI on demand, and keep the initial render fast for
    better Core Web Vitals. Every chart below is synthetic and deterministic.
  </p>
</header>

<!-- @let derives a single readable line from the selected trigger; @switch renders the
     matching explainer. Together they show the new control flow outside of @defer. -->
@let active = selectedNote();

<section class="demo-panel" aria-label="Trigger summary">
  <h2 class="demo-panel__title" id="pick-a-trigger">Pick a trigger</h2>
  <div class="trigger-bar" role="group" aria-label="Deferral triggers">
    @for (t of triggers; track t.kind) {
    <button
      foo-button
      [variant]="t.kind === selected() ? 'primary' : 'secondary'"
      size="sm"
      (click)="select(t.kind)"
      [attr.aria-pressed]="t.kind === selected()"
    >
      {{ t.label }}
    </button>
    }
  </div>

  <p class="trigger-summary">
    <foo-badge variant="brand" badgeSize="sm">&#64;defer ({{ active.label }})</foo-badge>
    @switch (selected()) { @case ('viewport') {
    <span>{{ active.blurb }}</span>
    } @case ('interaction') {
    <span>{{ active.blurb }}</span>
    } @case ('idle') {
    <span>{{ active.blurb }}</span>
    } @default {
    <span>{{ active.blurb }}</span>
    } }
  </p>
</section>

<!-- linkedSignal: derived-but-writable state, off the @defer track but on the modern-signals
     track. The selection follows its source list yet stays user-overridable. -->
<section class="demo-panel" aria-labelledby="linked-signal">
  <h2 class="demo-panel__title" id="linked-signal">linkedSignal · derived, but writable</h2>
  <p class="copy">
    A <code>computed</code> is read-only, which suits a value you only ever read. But a selection is
    both <em>derived</em> (it should default from its source) and <em>writable</em> (the user
    picks). <code>linkedSignal</code> is exactly that: it re-derives when its source changes, yet
    accepts a manual write that sticks until the next source change. Swap the dataset and the
    selection snaps back to the first region; pick a region and your choice holds.
  </p>

  <div class="linked-controls">
    <label class="linked-field">
      <span class="linked-field__label">Source dataset</span>
      <select
        foo-select
        fieldSize="sm"
        [value]="datasetId()"
        (change)="pickDataset($any($event.target).value)"
      >
        @for (set of datasets; track set.id) {
        <option [value]="set.id">{{ set.label }}</option>
        }
      </select>
    </label>

    <label class="linked-field">
      <span class="linked-field__label">Selected region</span>
      <select
        foo-select
        fieldSize="sm"
        [value]="selectedRegion()"
        (change)="pickRegion($any($event.target).value)"
      >
        @for (region of regions(); track region) {
        <option [value]="region">{{ region }}</option>
        }
      </select>
    </label>

    <button
      foo-button
      variant="ghost"
      size="sm"
      [disabled]="!isOverridden()"
      (click)="resetRegion()"
    >
      Reset to default
    </button>
  </div>

  <p class="linked-readout" role="status">
    <foo-badge variant="brand" badgeSize="sm">linkedSignal</foo-badge>
    <span>Region <strong>{{ selectedRegion() }}</strong> in {{ activeSet().label }}</span>
    @if (isOverridden()) {
    <foo-badge variant="warning" badgeSize="sm">overridden</foo-badge>
    } @else {
    <foo-badge variant="neutral" badgeSize="sm">source default</foo-badge>
    }
  </p>
</section>

<!-- 1. on viewport: skeleton placeholder, minimum-300ms loading, then the heavy child. -->
<section class="demo-panel" aria-label="Defer on viewport">
  <h2 class="demo-panel__title" id="on-viewport">1 · &#64;defer (on viewport)</h2>
  <p class="copy">
    The chart below renders only once it scrolls into view. Until then a skeleton
    (<code>&#64;placeholder</code>) holds its space. Once triggered,
    <code>&#64;loading (minimum 300ms)</code> shows while the chunk loads and stays up at least 300
    ms, so a fast load doesn't flicker.
  </p>

  <div class="scroll-window" tabindex="0" aria-label="Scrollable region">
    <p class="scroll-hint">Scroll down inside this box to trigger the deferred chart ↓</p>
    <div class="scroll-spacer"></div>

    @defer (on viewport) {
    <foo-card variant="raised">
      <foo-heavy-chart caption="Loaded on viewport" [seed]="33" />
    </foo-card>
    } @placeholder {
    <div class="skeleton" aria-hidden="true">
      <div class="skeleton__caption"></div>
      <div class="skeleton__chart"></div>
    </div>
    } @loading (minimum 300ms) {
    <div class="loading" role="status">Loading chart…</div>
    } @error {
    <foo-alert variant="danger" heading="Couldn’t load the chart">
      The deferred chunk failed to load. Try reloading the page.
    </foo-alert>
    }
  </div>
  <foo-source
    [snippets]="viewportSources"
    note="The page template holds all four triggers; this one is the on-viewport block. HeavyChart is referenced only inside it, so it builds into its own chunk and isn't requested until the placeholder scrolls into view."
  />
</section>

<!-- 2. on interaction: renders on the first click/focus of the trigger element. -->
<section class="demo-panel" aria-label="Defer on interaction">
  <h2 class="demo-panel__title" id="on-interaction">2 · &#64;defer (on interaction)</h2>
  <p class="copy">
    Nothing is fetched until you interact. Click the trigger and the chart's chunk loads on demand.
    It's the common pattern for a panel a user might never open.
  </p>

  @defer (on interaction(loadTrigger)) {
  <foo-card variant="raised">
    <foo-heavy-chart caption="Loaded on interaction" [seed]="7" />
  </foo-card>
  } @placeholder {
  <button #loadTrigger foo-button variant="secondary" class="interaction-trigger">
    Click to load the chart
  </button>
  } @loading {
  <div class="loading" role="status">Loading chart…</div>
  }
</section>

<!-- 3. on idle: renders when the browser is idle (requestIdleCallback). -->
<section class="demo-panel" aria-label="Defer on idle">
  <h2 class="demo-panel__title" id="on-idle">3 · &#64;defer (on idle)</h2>
  <p class="copy">
    With <code>on idle</code> Angular waits for the browser to finish its urgent work, then renders
    during an idle period. It's the default trigger and a good fit for non-critical extras.
  </p>

  @defer (on idle) {
  <foo-card variant="raised">
    <foo-heavy-chart caption="Loaded when idle" [seed]="99" />
  </foo-card>
  } @placeholder {
  <div class="skeleton" aria-hidden="true">
    <div class="skeleton__caption"></div>
    <div class="skeleton__chart"></div>
  </div>
  }
</section>

<!-- 4. on timer: renders after a fixed delay, showing the loading state first. -->
<section class="demo-panel" aria-label="Defer on timer">
  <h2 class="demo-panel__title" id="on-timer">4 · &#64;defer (on timer(2s))</h2>
  <p class="copy">
    A fixed <code>on timer(2s)</code> delay: you'll see the placeholder, then the loading state,
    then the chart after two seconds. Handy for staged reveals.
  </p>

  @defer (on timer(2s)) {
  <foo-card variant="raised">
    <foo-heavy-chart caption="Loaded after 2s" [seed]="51" />
  </foo-card>
  } @placeholder {
  <div class="skeleton" aria-hidden="true">
    <div class="skeleton__caption"></div>
    <div class="skeleton__chart"></div>
  </div>
  } @loading (minimum 300ms) {
  <div class="loading" role="status">Loading chart…</div>
  } @error {
  <foo-alert variant="danger" heading="Couldn’t load the chart">
    The deferred chunk failed to load.
  </foo-alert>
  }
</section>

2 · @defer (on interaction)

Nothing is fetched until you interact. Click the trigger and the chart's chunk loads on demand. It's the common pattern for a panel a user might never open.

3 · @defer (on idle)

With on idle Angular waits for the browser to finish its urgent work, then renders during an idle period. It's the default trigger and a good fit for non-critical extras.

4 · @defer (on timer(2s))

A fixed on timer(2s) delay: you'll see the placeholder, then the loading state, then the chart after two seconds. Handy for staged reveals.