Sub topic 10
Prefetching code before it is rendered
This chapter assumes you know standalone components, the imports array, and the basic @defer block with its triggers. It covers how to download a deferred chunk before the block renders.
Loading and rendering are separate steps
A @defer block does two things. It downloads the JavaScript chunk, and it renders the content. A normal trigger such as on viewport starts both. The user then waits for the download before they see anything.
Prefetching separates the steps. A prefetch trigger downloads the chunk early. The main trigger renders the block later, and it can render at once because the code is already in the browser.
Syntax
Add prefetch on or prefetch when to the @defer parameters. Separate it from the main trigger with a semicolon.
<button type="button" #showChart>Show chart</button>
@defer (on interaction(showChart); prefetch on idle) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
}When the browser becomes idle, Angular downloads the chunk for app-chart. When the user clicks the button, the block renders without a network wait.
Prefetch triggers
Prefetch accepts the same triggers as the main block, except where noted below.
| Prefetch trigger | The chunk downloads when |
|---|---|
| `prefetch on idle` | The browser reports it is idle. |
| `prefetch on immediate` | The page starts rendering, without waiting. |
| `prefetch on timer(2s)` | The given time has passed. |
| `prefetch on viewport(ref)` | The placeholder or the referenced element enters the viewport. |
| `prefetch on interaction(ref)` | The user clicks or focuses the element. |
| `prefetch on hover(ref)` | The user hovers over or focuses the element. |
| `prefetch when expr` | The expression becomes true. |
A prefetch trigger can differ from the main trigger. A common pairing is to render on a user action and prefetch earlier, based on a weaker signal.
A two-stage example
Here the block renders when the user clicks. The code is fetched as soon as the user hovers over the button. The gap between hover and click is often enough to finish a small download.
<button type="button" #openEditor>Open editor</button>
@defer (on interaction(openEditor); prefetch on hover(openEditor)) {
<app-editor />
} @placeholder {
<p>The editor opens when you press the button.</p>
}You can also drive the prefetch from state. This version prefetches once the user is signed in. It renders only when the user opens the panel.
@defer (when panelOpen(); prefetch when session.isSignedIn()) {
<app-settings-panel />
}What happens if the main trigger fires first
If the main trigger fires before the prefetch finishes, Angular does not download the chunk twice. The block waits for the download that is already running. If the download had not started, the main trigger starts it. In that case the block behaves as if no prefetch were set, so your @loading block can still appear.
Choosing a prefetch strategy
- Use
prefetch on idlefor content the user is likely to need soon. The cost is some bandwidth the user may never use. - Use
prefetch on hoverorprefetch on viewportfor content behind a click. These signals are stronger, so less data is wasted. - Use
prefetch on immediateonly for content you are almost sure to need. It competes with the rest of the page load. - Use
prefetch whenwhen intent shows up as application state, such as a signed-in session.
Common mistakes
- Writing the prefetch trigger without the main trigger. A
prefetchclause only controls the download. Without a main trigger, the block uses the defaulton idletrigger for rendering. - Forgetting the semicolon between the main and prefetch parts.
- Referencing a template variable that is not in scope, such as one inside another block, for
hoverorinteraction. - Expecting
prefetch whento fire again. Like a mainwhentrigger, it fires once.
Exercise
A page has a "Compare plans" button. Clicking it should show <app-plan-table />, which is a large standalone component. You want the click to feel instant, but you do not want to download the chunk for visitors who never move toward the button. Write the opening line of the @defer block and say why you chose that prefetch trigger.
Show answer
@defer (on interaction(compare); prefetch on hover(compare)), where compare is the template reference variable on the button (#compare). Hovering is a stronger sign of intent than idle time, so only visitors who approach the button download the chunk. The click then renders the block from code that is already loaded. Keyboard users get the same effect because hover also responds to focus.