Sub topic 9
Reloading and cancelling stale requests
This chapter assumes you can declare an httpResource with a signal-based request and read value(), status() and error() in a template. It covers two behaviors that follow from that: repeating a request on demand, and what happens to a request that is no longer wanted.
Reloading with reload()
An httpResource fetches again whenever a signal read inside its request function changes. Sometimes the inputs have not changed but the server data has, for example after a save or when the user presses a refresh button. Call reload() to repeat the current request.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface User {
id: number;
name: string;
}
@Component({
selector: 'app-user-card',
template: `
<button (click)="user.reload()" [disabled]="user.isLoading()">Refresh</button>
<button (click)="userId.update((id) => id + 1)">Next user</button>
<p>Status: {{ user.status() }}</p>
@if (user.hasValue()) {
<p>{{ user.value().name }}</p>
}
`,
})
export class UserCardComponent {
userId = signal(1);
user = httpResource<User>(() => `/api/users/${this.userId()}`);
}reload() returns a boolean. It returns true when a new request was started. It returns false when nothing happened, which is the case while the resource is idle or already in the loading state.
Loading versus reloading
The resource reports two different states for "a request is running":
loading: the first fetch, or a fetch caused by changed request inputs. The previous value is discarded, sovalue()isundefined(or yourdefaultValue).reloading: a fetch started byreload(). The previous value stays available while the new one arrives.
isLoading() is true in both states. Use status() when you need to tell them apart. For example, you can show a spinner in place of the content only during loading, and keep the old content visible during reloading.
| Trigger | Status while running | `value()` while running |
|---|---|---|
| First request | `loading` | `undefined` or `defaultValue` |
| A signal in the request changes | `loading` | `undefined` or `defaultValue` |
| `reload()` | `reloading` | Previous value |
Cancelling stale requests
Suppose the user clicks "Next user" three times quickly. Three requests start, but only the last one matters. When the request function produces a new request, the resource cancels the one still in flight. It does this by unsubscribing from the underlying HttpClient observable, which aborts the browser request. A late response from an older request can never overwrite the newer result.
Correction: the diagram must use the required fence form. Here it is:
Common errors
- Calling
reload()while the status isloadingand expecting a second request. It returnsfalseand does nothing. - Showing a full-page spinner whenever
isLoading()istrue. This hides the old content on every reload. Checkstatus() === 'loading'instead. - Reading
value()after an error. In recent Angular versions, readingvalue()in theerrorstate throws. Guard withhasValue()as in the example above.
Exercise
A resource shows a spinner when isLoading() is true. Users complain that the list flickers away each time they press Refresh, which calls reload(). What change keeps the old list on screen during a reload?
Show answer
Show the spinner only when status() === 'loading', and render the list whenever hasValue() is true. During reloading, isLoading() is true but the previous value is still available, so the list stays visible while the new data arrives.