الانتقال إلى المحتوى
FrontendAngular· 6 دقائق للقراءة

Not Every Server Request Is Reused in the Browser: The HttpClient Transfer Cache

Angular hands server responses to the browser only for eligible HttpClient requests made during the first render. Auth headers, POSTs, cache headers and mismatched URLs all mean a second fetch.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A store's product page is server-rendered, and its HTML already contains the name, the price and the reviews. Yet right after load, the Network panel shows the browser requesting the product, the reviews and the related items again, and the API now sees every page view twice. The team expected the transfer cache to prevent exactly this. The cause is the auth interceptor: it attaches an 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

The belief: the server already fetched everything the page needed, and SSR hands all of it to the browser, so a server-rendered app never calls the API twice for the same data.

The belief comes from a real feature that's on by default. 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

On the server, 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

By default, the guide says, 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.

That's the opening story. The exclusions exist so that one user's responses don't end up serialized into a page, and an interceptor that authenticates everything makes every request look user-specific.

TypeScript auth.interceptor.ts
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 }));};

The fix doesn't touch the cache settings. Public data goes out without credentials, so it qualifies on its own, and requests that really are personal keep their header and are fetched again in the browser, which is the safe outcome.

The browser has to ask the same question

A stored response is found by the request's details: in Angular 22's implementation, the method, the URL, the query parameters, the body and the response type. If the browser's request differs, it's a miss. Two common causes:

  • The server calls the API on an internal origin, such as http://api.internal:8080, and the browser on the public one. Provide HTTP_TRANSFER_CACHE_ORIGIN_MAP in 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

Even a cache hit isn't a full replay. No response headers are included by default, so code that reads 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

A 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.

Why the browser fetches againWhat to do
The call doesn't use HttpClientUse HttpClient, or give a resource() an id
It's a POST, even one that only readsincludePostRequests: true, for read-only POSTs such as GraphQL queries
It carries auth headers or credentialsKeep public reads anonymous instead of opting in globally
Cache-Control forbids caching, or the response sets a cookieFix the API headers if the data really is public
The browser's URL or parameters differMap origins on the server; keep parameters deterministic
It's sent after the app became stableNothing: that's outside the cache by design

How to apply it in your project

  1. Measure the duplicatesReload a few key server-rendered pages with the Network panel open, and list the API calls the browser repeats.
  2. Match each one to a ruleCheck its method, its headers after interceptors, its Cache-Control and its URL against the default rules.
  3. Keep anonymous reads anonymousScope auth interceptors to the endpoints that need credentials, so public data qualifies without global opt-ins.
  4. Align and complete the requestsMap server and browser origins with HTTP_TRANSFER_CACHE_ORIGIN_MAP, remove time-based parameters, and add the response headers your code reads to includeHeaders.

Key takeaways

  • The transfer cache reuses server responses only during the browser's initial render, until the app is stable.
  • By default only GET and HEAD without 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.

📚 المصادر

  1. Caching data when using HttpClient — Angular
  2. withHttpTransferCacheOptions — Angular
  3. HttpTransferCacheOptions — Angular
  4. HTTP_TRANSFER_CACHE_ORIGIN_MAP — Angular
  5. Caching resource data with SSR — Angular

التعليقات (0)