MCPcopy Index your code
hub / github.com/angular/angular / transferCacheInterceptorFn

Function transferCacheInterceptorFn

packages/common/http/src/transfer_cache.ts:252–316  ·  view source on GitHub ↗
(
  req: HttpRequest<unknown>,
  next: HttpHandlerFn,
)

Source from the content-addressed store, hash-verified

250}
251
252export function transferCacheInterceptorFn(
253 req: HttpRequest<unknown>,
254 next: HttpHandlerFn,
255): Observable<HttpEvent<unknown>> {
256 const options = inject(CACHE_OPTIONS);
257 if (!canUseOrCacheRequest(req, options)) {
258 return next(req);
259 }
260
261 const transferState = inject(TransferState);
262 const originMap = inject(HTTP_TRANSFER_CACHE_ORIGIN_MAP, {optional: true});
263 const requestUrl =
264 typeof ngServerMode !== 'undefined' && ngServerMode && originMap
265 ? mapRequestOriginUrl(req.url, originMap)
266 : req.url;
267 const storeKey = makeCacheKey(req, requestUrl);
268
269 const cachedResponse = retrieveStateFromCache(
270 req,
271 options,
272 transferState,
273 /** originMap */ null,
274 storeKey,
275 /** skipUseCacheChecks */ true,
276 );
277
278 if (cachedResponse) {
279 return of(cachedResponse);
280 }
281
282 const event$ = next(req);
283 if (typeof ngServerMode !== 'undefined' && ngServerMode) {
284 // Request not found in cache. Make the request and cache it if on the server.
285 return event$.pipe(
286 tap((event: HttpEvent<unknown>) => {
287 if (event instanceof HttpResponse) {
288 const {headers, body, status, statusText} = event;
289
290 // Only cache successful HTTP responses that do not have Cache-Control
291 // directives that forbid shared caching (no-store or private) and do not
292 // carry a Set-Cookie header. A Set-Cookie header marks the response as
293 // user-specific.
294 if (hasUncacheableCacheControl(headers) || hasSetCookieHeader(headers)) {
295 return;
296 }
297
298 const {transferCache: requestOptions, responseType} = req;
299 const headersToInclude = getHeadersToInclude(options, requestOptions);
300
301 transferState.set<TransferHttpResponse>(storeKey, {
302 [BODY]:
303 responseType === 'arraybuffer' || responseType === 'blob' ? toBase64(body) : body,
304 [HEADERS]: getFilteredHeaders(headers, headersToInclude),
305 [STATUS]: status,
306 [STATUS_TEXT]: statusText,
307 [REQ_URL]: requestUrl,
308 [RESPONSE_TYPE]: responseType,
309 });

Callers 1

runInterceptorFunction · 0.90

Calls 12

injectFunction · 0.90
canUseOrCacheRequestFunction · 0.85
mapRequestOriginUrlFunction · 0.85
makeCacheKeyFunction · 0.85
retrieveStateFromCacheFunction · 0.85
hasSetCookieHeaderFunction · 0.85
getHeadersToIncludeFunction · 0.85
toBase64Function · 0.85
getFilteredHeadersFunction · 0.85
setMethod · 0.65
nextFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…