Sub topic 5
Preventing flicker with minimum and after
This chapter assumes you can already write a basic @defer block and know the roles of @placeholder, @loading and @error. It also assumes you know that a trigger starts the load. Here you will learn the two timing parameters, minimum and after, which control how long those blocks stay on screen.
The flicker problem
A deferred block moves through up to four visible states. It starts in the placeholder. When the trigger fires, the loading block can appear while the chunk downloads. Then the complete content or the error block replaces it.
Each switch replaces DOM nodes. When the chunk comes from a fast network or the browser cache, the whole sequence can take a few milliseconds. The user sees a placeholder flash, a spinner flash, and then the content. Nothing is broken, but the page looks unstable.
The opposite case also exists. A spinner that appears the moment loading starts is noise if the content arrives 40 ms later.
Angular gives you two parameters to smooth this out:
| Parameter | Used on | Meaning |
|---|---|---|
| `minimum` | `@placeholder`, `@loading` | The least time the block must stay visible once it has been rendered. |
| `after` | `@loading` only | How long to wait after loading starts before showing the loading block at all. |
Both take a time value written as a number followed by ms or s, such as 500ms or 1s. Neither changes when or how the chunk is downloaded. They change only when the visible state switches.
Syntax
The parameters go in parentheses after the block keyword. In @loading, separate the two parameters with a semicolon. The order does not matter.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder (minimum 500ms) {
<p>Comments load when this area is visible.</p>
} @loading (after 100ms; minimum 1s) {
<app-spinner />
} @error {
<p>Comments could not be loaded.</p>
}The @error block takes no timing parameters. A parameter that does not belong to a block, such as after on @placeholder, is a compile error.
minimum on @placeholder
The placeholder's minimum starts counting when the placeholder is rendered. If the trigger fires before that time has passed, Angular keeps the placeholder on screen until the minimum has elapsed. Only then does it move to the next state.
This is most useful with triggers that can fire almost immediately, such as on idle, or on viewport when the block is already in view on page load. Without minimum, the placeholder could appear for one frame and disappear. That flash is worse than showing nothing, because the user notices something changed but cannot read it.
If the trigger fires after the minimum has already passed, the parameter has no effect. The placeholder is replaced as soon as the next state is ready.
after and minimum on @loading
These two parameters work at different ends of the loading state.
after delays the loading block. The timer starts when loading begins. If the chunk arrives before the timer ends, the loading block is never rendered. The placeholder goes straight to the content. This gets rid of spinners that would flash for a few milliseconds.
minimum holds the loading block once it is shown. If it appeared because loading took longer than after, it stays for at least the minimum time, even if the chunk arrives right afterwards. This prevents a spinner from appearing and vanishing in a blink.
The two work as a pair. after says "do not show a spinner unless this is slow". minimum says "if you did show one, keep it long enough to read".
Worked timeline
Suppose a block uses @loading (after 100ms; minimum 1s). All times below are counted from the moment loading starts.
| Chunk arrives at | What the user sees |
|---|---|
| 60 ms | The placeholder is replaced directly by the content. The loading block is never rendered. |
| 100 ms or later, for example 300 ms | The loading block appears at 100 ms. It stays until 1100 ms (100 ms plus the 1 s minimum). The content appears at 1100 ms. |
| 2500 ms | The loading block appears at 100 ms. The content replaces it at 2500 ms, because the minimum has already passed. |
The second row is the one that surprises people. The content is ready at 300 ms, but the user waits until 1100 ms to see it. The minimum value is a deliberate delay, so it has a cost. Keep it short.
A complete example
The component below defers a chart behind a button. The placeholder is stable for half a second, and the spinner appears only for slow loads. The ChartComponent and SpinnerComponent files are assumed to exist as standalone components.
import { Component } from '@angular/core';
import { ChartComponent } from './chart.component';
import { SpinnerComponent } from './spinner.component';
@Component({
selector: 'app-report',
imports: [ChartComponent, SpinnerComponent],
template: `
<button type="button" #showChart>Show chart</button>
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder (minimum 300ms) {
<p class="chart-slot">Press the button to see the chart.</p>
} @loading (after 150ms; minimum 600ms) {
<app-spinner class="chart-slot" />
} @error {
<p class="chart-slot">The chart could not be loaded.</p>
}
`,
styles: `
.chart-slot { min-height: 320px; }
`,
})
export class ReportComponent {}Here the click is a user action, so the placeholder has usually been visible far longer than 300 ms. The placeholder minimum rarely matters for this trigger. The after 150ms is the useful part: on a fast connection the user clicks and the chart appears with no spinner. The .chart-slot class gives all three states the same height, which is covered below.
Choosing values
There are no official recommended numbers, but these rules work in practice:
- Pick
afterfrom the time users start to feel waiting. Values between 100 ms and 200 ms hide most cache hits and fast loads. - Keep the loading
minimumbetween 500 ms and 1 s. Anything below about 300 ms is too brief to read as a state. Anything above 1 s makes fast loads feel slow. - Use a placeholder
minimumonly when the trigger can fire very early. Foron interactionandon hoverit is usually unnecessary. - Do not set a
minimumon blocks that hold no visible content. If the placeholder is empty, delaying its removal only delays the page.
Interaction with prefetching
When you add prefetch on idle or another prefetch trigger, the chunk may already be downloaded by the time the main trigger fires. In that case there is nothing to wait for, so the @loading block is not needed and the after delay never has to elapse. The block goes from the placeholder to the content. The placeholder's minimum can still hold the swap if the trigger fired early.
This is a good reason to combine the two techniques. Prefetching makes the loading state rare, and after and minimum handle the cases where it still happens.
Layout shift is a separate problem
Timing parameters control when states switch. They do nothing about the size of what is shown. If the placeholder is 20 px tall and the content is 400 px tall, the page jumps when the content arrives, no matter how carefully you set minimum. Give the placeholder, loading and error blocks the same reserved space as the final content, as .chart-slot does above. A later chapter covers this in detail.
Common mistakes
- Putting
afteron@placeholder. Only@loadingacceptsafter. The compiler reports an error. - Using a comma between parameters. In
@loading, parameters are separated with a semicolon:(after 100ms; minimum 1s). - Leaving out the unit.
minimum 500is invalid. Write500msor0.5s. - Expecting
afterto delay the download. It delays only the loading block. The download starts as soon as the trigger fires, or earlier with prefetching. - Testing only on a fast machine. On localhost, chunks arrive in a few milliseconds and the loading block never shows. Use the browser's network throttling in DevTools to see the slow path.
Exercise
A block uses @placeholder (minimum 500ms) and @loading (after 100ms; minimum 1s). The trigger fires 600 ms after the placeholder was rendered, and the chunk then downloads in 50 ms. What does the user see between the trigger and the content?
Show answer
Nothing but the placeholder, then the content. The placeholder's 500 ms minimum already passed before the trigger fired, so it does not delay anything. The chunk arrives after 50 ms, which is before the 100 ms after delay, so the loading block is never rendered. The content replaces the placeholder at about 650 ms after the placeholder was rendered.
Exercise
With @loading (after 100ms; minimum 1s), the chunk takes 300 ms to download. At what time, counted from the start of loading, does the loading block appear, and when does the content appear? Why is the content not shown at 300 ms?
Show answer
The loading block appears at 100 ms. The content appears at 1100 ms. The loading block was shown at 100 ms, so its 1 s minimum keeps it on screen until 1100 ms, even though the chunk arrived at 300 ms.
Exercise
Why does adding prefetch on idle often make the @loading block unnecessary, and which parameter would you still keep on @placeholder?
Show answer
Prefetching downloads the chunk before the main trigger fires, so when the trigger fires there is nothing to wait for and the loading state is skipped. You would keep the placeholder minimum if the main trigger can fire very soon after the placeholder renders, such as on viewport for a block already in view, so the placeholder does not flash for a single frame.