Sub topic 6
Avoiding duplicate requests with the HTTP transfer cache
This chapter assumes you know HttpClient basics, standalone ApplicationConfig, and the SSR and hydration setup from earlier chapters. You should also know that the server renders the page first and the browser then hydrates it.
The duplicate request problem
When a component loads data in ngOnInit, the code runs twice with SSR. The server runs it to produce HTML. The browser runs it again after hydration. The same URL gets fetched two times, and the browser may briefly show different data than the server rendered, which can cause flicker.
Angular avoids this with a transfer cache. The server records each HttpClient response and embeds it in the HTML it sends. When the browser makes the same request, HttpClient returns the embedded response and does not touch the network.
It is on by default
The transfer cache is enabled automatically when you call provideClientHydration(). The shared config from the setup chapter already has everything it needs.
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withFetch()),
provideClientHydration(withEventReplay()),
],
};A component that uses HttpClient needs no extra code:
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { toSignal } from '@angular/core/rxjs-interop';
@Component({
selector: 'app-product-list',
template: `
@for (p of products(); track p.id) {
<p>{{ p.name }}</p>
}
`,
})
export class ProductListComponent {
private http = inject(HttpClient);
products = toSignal(
this.http.get<{ id: number; name: string }[]>('/api/products'),
{ initialValue: [] },
);
}The server fetches /api/products once. In the browser, the same GET is answered from the embedded data.
What gets cached
By default the cache applies under these rules:
| Rule | Default behavior |
|---|---|
| Methods | `GET` and `HEAD` only. `POST` is not cached. |
| Requests with auth headers | Skipped (`Authorization` or `Proxy-Authorization`). |
| Headers in cached response | Dropped, unless you opt in. |
| Lifetime | Only until the app becomes stable in the browser. |
The last rule matters. After hydration finishes and the app is stable, the cache is switched off. Later requests, such as a user clicking "refresh", go to the network as normal. The cache only removes the duplicate that happens during startup.
Customizing the cache
Pass options to withHttpTransferCacheOptions inside provideClientHydration.
import { provideClientHydration, withHttpTransferCacheOptions } from '@angular/platform-browser';
provideClientHydration(
withHttpTransferCacheOptions({
includePostRequests: true,
includeRequestsWithAuthHeaders: false,
filter: (req) => !req.url.includes('/api/live'),
}),
);includePostRequestscachesPOSTrequests too. Use it only when thePOSTis a read, as in some search or GraphQL APIs.includeRequestsWithAuthHeaderscaches requests that carry auth headers. Leave it off unless the data is safe to embed in the HTML.filterreturnsfalsefor requests you want to exclude.includeHeaderslists response headers to keep.
You can also opt out for a single request with the transferCache option: this.http.get(url, { transferCache: false }).
Common errors
- Requests still run twice. The URL, method, or parameters differ between server and browser. The cache key is built from the method, URL with parameters, and body. A relative URL on the server may resolve differently than in the browser.
- Requests still run twice for non-GET calls.
POSTis excluded by default. EnableincludePostRequestsonly if it is safe. - Fresh data never appears. Requests made before stability use the embedded copy. For data that must be current, set
transferCache: falseon that request. - Missing
withFetch(). Angular recommends the fetch backend for SSR. Without it, you may see a warning about the XHR backend.
When to use TransferState instead
The transfer cache covers HttpClient only. For values computed on the server that do not come from HttpClient, such as a database lookup, use TransferState directly with a key you define.
Exercise
A page loads /api/products with HttpClient.get in a component. After enabling SSR and hydration, the browser still calls /api/products on startup. The request carries an Authorization header. Why does this happen, and what are two ways to change it?
Show answer
The transfer cache skips requests with Authorization or Proxy-Authorization headers by default, so the browser repeats the call. You can set includeRequestsWithAuthHeaders: true in withHttpTransferCacheOptions, but only if the data is safe to embed in the page source. Alternatively, move the call so the server does not need the header, such as a public endpoint, or accept the second request.
Exercise
Why does the transfer cache stop working after the app becomes stable in the browser?
Show answer
Its purpose is to remove the duplicate request made during startup. Once the app is stable, new requests are user-driven and need fresh data, so they go to the network.