Sub topic 4
Showing state with @placeholder, @loading and @error
A @defer block does not render its content right away. For some time the user sees nothing from it, then the chunk downloads, and then the content appears. Three optional sub-blocks let you control what the user sees at each stage: @placeholder, @loading and @error. This chapter covers what each block does, when it shows, and the mistakes that make a deferred view behave unexpectedly.
What you need before this chapter
This chapter assumes you can write a standalone component with an imports array and use a basic @defer { ... } block. It also assumes you know the first-block example from the previous chapter, where a standalone component used only inside @defer moves into a lazy chunk. You don't need to know all the triggers yet. Examples here use on viewport and on interaction without explaining their internals. Those triggers have their own chapters. The minimum and after timing parameters appear in the examples, but the next chapter covers them in detail.
The states of a deferred block
A deferred block is always in one of four states. The three sub-blocks map to three of them.
| State | Sub-block that renders | When it is active |
|---|---|---|
| Placeholder | `@placeholder` | From first render until the trigger fires |
| Loading | `@loading` | While the chunk is downloading |
| Complete | The main `@defer` content | After the chunk has loaded |
| Error | `@error` | If the chunk could not be loaded |
The block moves forward only. After it reaches Complete, it never goes back to Placeholder, even if the trigger condition stops being true. After it reaches Error, it stays there.
All three sub-blocks are optional. Each may appear at most once per @defer, and each must directly follow the preceding block with nothing between them.
@placeholder
The placeholder is what the user sees before loading starts. Without one, the area is empty until the content arrives.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-placeholder">Comments load when this area is visible.</div>
}Three facts about the placeholder matter in practice.
- Its content is not deferred. The placeholder renders on the first pass, so everything it uses must already be in the main bundle. Any component you use in a placeholder is imported eagerly.
- It can be the trigger target. For
on viewport,on interactionandon hoverwithout a parameter, the placeholder element is the thing the trigger watches. In that case the placeholder must have exactly one root element, and the compiler reports an error if it does not. If you reference another element, such ason interaction(showChart), the placeholder can be anything. - It accepts
minimum.@placeholder (minimum 500ms)keeps the placeholder on screen for at least 500 ms once it has been shown. This stops the placeholder from flashing away if the chunk loads instantly. The next chapter covers when to use it.
@loading
The loading block renders while the chunk downloads. It runs between the trigger firing and the content being ready.
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
} @loading (after 100ms; minimum 1s) {
<app-spinner />
}The two optional parameters are separated by a semicolon.
after 100msdelays the loading block. If the chunk arrives within 100 ms, the user never sees it.minimum 1skeeps the loading block on screen for at least one second once it appears.
Both timers start when loading begins, not when the page renders. Like the placeholder, the loading block's dependencies are eager, so a spinner component stays in the main bundle.
The loading block is skipped when the code is already available. If you have prefetched the chunk (see the prefetching chapter) or another block loaded it earlier, the state goes straight from Placeholder to Complete.
@error
The error block renders when the chunk cannot be downloaded or evaluated. Typical causes are a network failure, a server returning an error for the chunk file, or a stale deployment where the old chunk file no longer exists.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @error {
<p>Comments could not be loaded. Reload the page to try again.</p>
}Angular does not retry on its own. After the block enters the error state, it stays there. If you want a retry, you must build it yourself, and the simplest option for most pages is to tell the user to reload. As with the other sub-blocks, anything used inside @error is eager.
All three together
This component combines every block. It assumes a CommentsComponent with a required postId input and a SpinnerComponent, both standalone.
import { Component, input } from '@angular/core';
import { CommentsComponent } from './comments.component';
import { SpinnerComponent } from './spinner.component';
@Component({
selector: 'app-post',
imports: [CommentsComponent, SpinnerComponent],
styles: `
.comments-slot { min-height: 240px; }
`,
template: `
<article>
<h1>Post {{ postId() }}</h1>
</article>
@defer (on viewport) {
<section class="comments-slot">
<app-comments [postId]="postId()" />
</section>
} @placeholder (minimum 500ms) {
<section class="comments-slot">
<p>Comments load when this area is visible.</p>
</section>
} @loading (after 100ms; minimum 1s) {
<section class="comments-slot">
<app-spinner />
</section>
} @error {
<section class="comments-slot">
<p>Comments could not be loaded.</p>
</section>
}
`,
})
export class PostComponent {
postId = input.required<number>();
}Notice three things:
CommentsComponentappears only inside the main@defercontent, so the compiler moves it into the lazy chunk.SpinnerComponentis used only in@loading. Because sub-block content is eager, it stays in the main bundle even though it is inside a@defer.- Every block uses the same
comments-slotwrapper with amin-height. This keeps the page height steady as the block changes state. Layout shift gets its own chapter, but this habit is worth adopting from the start.
Each block has a single root element, which also keeps the placeholder valid for the on viewport implicit trigger.
Rules and common errors
A deferred component also used in a sub-block is no longer deferred
The compiler defers a dependency only if every use of it in the template is inside @defer main content. If you put <app-comments /> in the placeholder as well, or elsewhere in the template outside the block, it stays in the main bundle and the @defer saves nothing. The build gives no error. You can check by looking at the lazy chunk files the build produces.
Placeholder without a single root element
With an implicit trigger, a placeholder such as <h2>Title</h2><p>Text</p> has two roots, so the compiler cannot pick an element to observe. Wrap the content in one element, or give the trigger an explicit reference.
Expecting the loading block to always show
On a fast connection, or with a cached chunk, the download finishes quickly. With after 100ms, the user never sees the spinner, and that is the intended outcome. If you are testing the loading block and cannot see it, throttle the network in your browser's developer tools instead of removing the after parameter.
Treating @error as a retry mechanism
@error only reports the failure. If the page must recover, add your own control, such as a button that calls location.reload(), or design the feature so the page remains usable without the deferred part.
Duplicate or misplaced sub-blocks
Two @placeholder blocks on one @defer, or a sub-block separated from its @defer by other markup, is a compile error. Place sub-blocks directly after the closing brace of the preceding block.
Bindings inside a placeholder
The placeholder renders in the same component context as the rest of the template, so it can read your signals and fields. However, it does not receive the deferred component's inputs or state. It only knows what the parent component knows.
Seeing each state in the browser
You can force each state without changing your code.
- Run the app with
ng serveand open the browser developer tools. - In the Network tab, set throttling to a slow profile. This makes the loading block visible.
- To see the error block, find the lazy chunk request after the first load, use "Block request URL" on it, and reload the page. Then trigger the block.
The chunk files have generated names, so look for a small JavaScript file requested right when the trigger fires.
Exercise
A template has @defer (on viewport) { <app-map /> } @placeholder { <app-map-preview /> <p>Scroll to load</p> }. Name two problems with this code.
Show answer
First, the placeholder has two root elements (<app-map-preview /> and <p>), so the implicit on viewport trigger has no single element to observe and the compiler reports an error. Second, app-map-preview is imported eagerly because placeholder content is never deferred. If the preview is heavy, it loads with the main bundle. The fix is to wrap both elements in one <div> and keep the preview component light.
Exercise
You configure @loading (after 200ms; minimum 1s). The chunk downloads in 150 ms. What does the user see between the trigger firing and the content appearing, and does the 1 s minimum apply?
Show answer
The user sees the placeholder until the content appears. The loading block never renders, because the download finished before the 200 ms after delay ended. The minimum value only applies once the loading block has actually been shown, so it has no effect here.
Exercise
The user's network drops while the chunk is downloading, and the block enters the error state. The network then comes back. Does the block try again by itself?
Show answer
No. Angular does not retry a failed chunk load. The block stays in the error state, so you need a reload or custom recovery logic if you want the content to appear.