Sub topic 5
Preventing flicker with minimum and after
This chapter covers the two timing parameters on @placeholder and @loading. It assumes you can write a basic @defer block and know what the @placeholder, @loading and @error sub-blocks do. It also assumes you know how standalone components appear in the imports array.
The flicker problem
A deferred chunk can arrive in a few milliseconds or in several seconds. On a fast connection the chunk may load almost instantly. The user then sees a placeholder, a spinner and the final content flash by in quick succession. This looks like a glitch even though nothing is wrong.
Angular gives you two timing parameters to smooth this out:
minimumsets the shortest time a block stays on screen once it appears.aftersets how long Angular waits before it shows a block at all.
Both take a duration such as 500ms or 1s. Milliseconds (ms) and seconds (s) are the accepted units.
Where each parameter applies
| Block | `minimum` | `after` |
|---|---|---|
| `@placeholder` | Yes | No |
| `@loading` | Yes | Yes |
| `@error` | No | No |
The placeholder is shown immediately, so it has nothing to wait for. The loading block can be delayed, because many loads finish quickly and a spinner would only get in the way.
minimum on @placeholder
@placeholder (minimum 500ms) keeps the placeholder visible for at least 500 ms. If the chunk is ready sooner, Angular still waits until the 500 ms have passed before it swaps in the content. The timer starts when the placeholder renders.
after and minimum on @loading
@loading (after 100ms; minimum 1s) combines both parameters. Separate them with a semicolon.
after 100ms: the loading block appears only if loading is still in progress 100 ms after it began. A load that finishes in 60 ms never shows the spinner.minimum 1s: once the loading block appears, it stays for at least one second.
The after timer starts when the loading state begins, which is when the trigger fires and the download starts. If you also set minimum on the placeholder, the transition to loading waits for that minimum first.
A complete example
@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>
}Timeline of a slow and a fast load
The diagram follows the states of one block. The transitions depend on the timing parameters.
Common mistakes
- Setting
afteron@placeholder. It is not supported there. Onlyminimumis valid. - Using a comma between parameters. Use a semicolon:
after 100ms; minimum 1s. - Choosing very long minimums. A 3 s minimum delays content that has already arrived. Keep values short, typically 100 ms to 1 s.
- Expecting timing on
@error. The error block takes no parameters and renders as soon as the load fails. - Adding
@loadingtiming without a@loadingblock. The parameters only apply to blocks that exist.
Checking your values
Throttle the network in your browser's developer tools to a slow profile. Reload the page and scroll to the deferred area. Watch whether the placeholder holds long enough to be read, whether the spinner shows on the slow load and whether the content swaps in without a blink. Then switch to an unthrottled network and confirm the spinner no longer appears.
Timing values reduce flicker, but they do not prevent layout shift. If the placeholder and the final content differ in size, the page still jumps. A later chapter covers that.
Exercise
A deferred block uses @placeholder (minimum 400ms) and @loading (after 150ms; minimum 800ms). The chunk finishes downloading 100 ms after loading begins. What does the user see?
Show answer
The loading block never appears, because the load finished before the 150 ms after delay. The placeholder has already stayed for its 400 ms minimum, so the deferred content replaces it once the chunk is ready.
Exercise
Why does @error (minimum 1s) not work, and which blocks accept after?
Show answer
@error accepts no timing parameters and renders as soon as the load fails. Only @loading accepts after. @placeholder and @loading accept minimum.