
Not Every Server Request Is Reused in the Browser: The HttpClient Transfer Cache
- كاتب موثّق
- Angular 22
محتويات المقال
Authorization header to every request, the public catalog included, and requests carrying that header are left out of the transfer cache by default.
The common misconception
provideClientHydration() enables HttpClient response caching on the server and transfers that cache to the client "to avoid extra HTTP requests", in the words of its API docs. In a starter app, where every request is an anonymous GET, the Network panel confirms it: no duplicates. Real apps add interceptors, POST-based queries, cache headers and separate API hostnames, and each of those quietly opts a request out.
What actually happens
A one-time handoff, not an HTTP cache
HttpClient records the responses of eligible requests, and Angular serializes them into the HTML through . In the browser, during the initial render, HttpClient looks for a matching entry before it sends a request and returns the stored response if it finds one. The SSR guide adds the limit that matters: HttpClient stops using the cache once the app becomes stable in the browser. Requests made later, after a client-side navigation or from a polling timer, go to the network. It exists to make cheap, nothing more.
Eligibility is decided per request
HttpClient caches GET and HEAD requests that carry no Authorization, Proxy-Authorization or Cookie header and aren't sent with credentials. It also skips requests and responses whose Cache-Control says no-store, no-cache or private, and responses that set a cookie. In Angular 22's implementation, that check sees the request as it leaves your interceptors, so a header an interceptor adds counts exactly as if you had written it yourself.
import { HttpInterceptorFn } from '@angular/common/http';import { inject } from '@angular/core';export const authInterceptor: HttpInterceptorFn = (req, next) => { const token = inject(Session).token(); if (!token) return next(req); // Every request gets Authorization, public reads included, so // none is eligible for the transfer cache: each is sent twice const headers = { Authorization: `Bearer ${token}` }; return next(req.clone({ setHeaders: headers }));};import { HttpInterceptorFn } from '@angular/common/http';import { inject } from '@angular/core';export const authInterceptor: HttpInterceptorFn = (req, next) => { const token = inject(Session).token(); // Catalog reads stay anonymous, so the server's responses are // serialized into the page and reused during hydration if (!token || req.url.includes('/api/catalog/')) return next(req); const headers = { Authorization: `Bearer ${token}` }; return next(req.clone({ setHeaders: headers }));};The browser has to ask the same question
The server calls the API on an internal origin, such as http://api.internal:8080, and the browser on the public one. ProvideHTTP_TRANSFER_CACHE_ORIGIN_MAPin the server config only, mapping the server origin to the browser one; Angular throws if it finds that token in the browser.A parameter is computed at request time, such as a timestamp to bust caches or a value derived from the clock. Keep request parameters deterministic during the first render.
Headers stay on the server
X-Total-Count from a cached response gets nothing. List the headers you need in includeHeaders, globally or per request with the transferCache option. In development, Angular logs a warning when you read a header that wasn't transferred.
Not everything goes through HttpClient
fetch() call, a vendor SDK or a resource() with its own loader isn't part of this cache at all. For a resource(), there's an id option: the server stores the resolved value in TransferState, and the browser starts the resource in the resolved state instead of loading it again.
HttpClient | HttpClient, or give a resource() an id |
POST, even one that only reads | includePostRequests: true, for read-only POSTs such as GraphQL queries |
Cache-Control forbids caching, or the response sets a cookie | |
How to apply it in your project
Measure the duplicates Reload a few key server-rendered pages with the Network panel open, and list the API calls the browser repeats. Match each one to a rule Check its method, its headers after interceptors, its Cache-Controland its URL against the default rules.Keep anonymous reads anonymous Scope auth interceptors to the endpoints that need credentials, so public data qualifies without global opt-ins. Align and complete the requests Map server and browser origins with HTTP_TRANSFER_CACHE_ORIGIN_MAP, remove time-based parameters, and add the response headers your code reads toincludeHeaders.
Key takeaways
The transfer cache reuses server responses only during the browser's initial render, until the app is stable. By default only GETandHEADwithout auth headers, credentials or no-cache directives qualify.Interceptors count: a header they add can make every request ineligible. The browser must send the same method, URL, parameters and body; map origins on the server. Opt-ins serialize responses into HTML, so use them only for data that's the same for every user.
التعليقات (0)