Sub topic 11
Avoiding layout shift and choosing what to defer
This chapter assumes you know standalone components, the @defer syntax with @placeholder, @loading and @error, and the common triggers. It covers two decisions: how to keep the page from jumping when deferred content arrives, and which parts of a template are worth deferring.
Why deferred content causes layout shift
A deferred block swaps one piece of DOM for another. The placeholder is removed and the real content is inserted. If the two have different heights, everything below them moves. Browsers measure this movement as Cumulative Layout Shift (CLS), and it hurts both user experience and Core Web Vitals scores.
The shift is worst with viewport and interaction triggers. The user is already looking at or reading the page when the swap happens.
Reserve the space
The most reliable fix is to give the placeholder the same footprint as the final content. Set an explicit height, or use aspect-ratio for media.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-slot"></div>
} @loading (after 100ms; minimum 1s) {
<div class="comments-slot"><app-spinner /></div>
}.comments-slot {
min-height: 320px;
}Use the same wrapper class in the @placeholder and @loading blocks. The block then keeps its size through each state, and only its content changes.
Do not rely on the placeholder being small and "growing into" the content. If the content height varies (for example, a list of comments), reserve a reasonable minimum and accept a smaller shift.
Watch the viewport trigger
With on viewport, the trigger fires when the placeholder enters the viewport. If the placeholder is tiny, the deferred content may be loaded and rendered while the user is looking at it, and the page below jumps. Two rules help:
- A placeholder with a real size gives the observer something meaningful to watch.
- A
@placeholderis required foron viewportand foron interactionandon hoverwithout a reference, because Angular needs an element to observe.
Content that is already inside the viewport on page load should not use on viewport. It will load right away, and you add a placeholder-to-content swap on the critical path for no gain.
Choose what to defer
Deferring is a trade-off. You reduce the initial bundle, but you add a network request later and a visible state change. Use these criteria.
| Defer it | Keep it eager |
|---|---|
| Below the fold (comments, related items, footers) | Above the fold content, especially the largest visible element |
| Heavy dependencies (charts, editors, maps) | Small components with few dependencies |
| Behind a user action (dialogs, tabs, "show more") | Content needed for the first meaningful interaction |
| Rarely used or conditional (admin panels, premium features) | Content important for search engines when you do not use SSR |
Two further checks:
- Measure the chunk. If the deferred code is only a few kilobytes, the extra request may cost more than it saves. Check the build output for the size of the new lazy chunk.
- Check eager references. A component is only moved into a lazy chunk if nothing else in the same file uses it outside a
@deferblock. A reference in the component's class, a template outside@defer, or aviewChildquery on it keeps it in the main bundle.
Avoid nesting defaults
Do not wrap the whole page in one @defer. Users then see a blank placeholder, and you have only moved the work later. Defer several independent regions with the triggers that suit each one, and prefetch where a user action is likely.
<button type="button" #openEditor>Edit</button>
@defer (on interaction(openEditor); prefetch on hover(openEditor)) {
<app-editor />
} @placeholder {
<div class="editor-slot"></div>
}Prefetching on hover downloads the chunk before the click, so the swap happens sooner and the loading state rarely appears.
Exercise
A page has a hero image at the top, a related-articles list far below, and a chart that appears after a "Show chart" button is clicked. Which of the three would you defer, which trigger would you use for each, and how would you avoid layout shift for the related-articles list?
Show answer
Keep the hero image eager because it is above the fold and is likely the largest visible element. Defer the related-articles list with on viewport and give the placeholder a min-height that matches the list, so the content below does not move. Defer the chart with on interaction(showChart) and add prefetch on idle or prefetch on hover(showChart) so the chunk is usually ready when the user clicks.