MCPcopy Create free account
hub / github.com/angular/angular / transferCacheInterceptorFn

Function transferCacheInterceptorFn

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

Source from the content-addressed store, hash-verified

278}
279
280export function transferCacheInterceptorFn(
281 req: HttpRequest<unknown>,
282 next: HttpHandlerFn,
283): Observable<HttpEvent<unknown>> {
284 const options = inject(CACHE_OPTIONS);
285 if (!canUseOrCacheRequest(req, options)) {
286 return next(req);
287 }
288
289 const transferState = inject(TransferState);
290 const originMap = inject(HTTP_TRANSFER_CACHE_ORIGIN_MAP, {optional: true});
291 const requestUrl =
292 typeof ngServerMode !== 'undefined' && ngServerMode && originMap
293 ? mapRequestOriginUrl(req.url, originMap)
294 : req.url;
295 const storeKey = makeCacheKey(req, requestUrl);
296
297 const cachedResponse = retrieveStateFromCache(
298 req,
299 options,
300 transferState,
301 /** originMap */ null,
302 storeKey,
303 /** skipUseCacheChecks */ true,
304 );
305
306 if (cachedResponse) {
307 return of(cachedResponse);
308 }
309
310 const event$ = next(req);
311 if (typeof ngServerMode !== 'undefined' && ngServerMode) {
312 // Request not found in cache. Make the request and cache it if on the server.
313 return event$.pipe(
314 tap((event: HttpEvent<unknown>) => {
315 if (event instanceof HttpResponse) {
316 const {headers, body, status, statusText} = event;
317
318 // Only cache successful HTTP responses that are not non-cacheable.
319 if (
320 !options.includeNonCacheableRequests &&
321 (hasUncacheableCacheControl(headers) || hasSetCookieHeader(headers))
322 ) {
323 return;
324 }
325
326 const {transferCache: requestOptions, responseType} = req;
327 const headersToInclude = getHeadersToInclude(options, requestOptions);
328
329 transferState.set<TransferHttpResponse>(storeKey, {
330 [BODY]:
331 responseType === 'arraybuffer' || responseType === 'blob' ? toBase64(body) : body,
332 [HEADERS]: getFilteredHeaders(headers, headersToInclude),
333 [STATUS]: status,
334 [STATUS_TEXT]: statusText,
335 [REQ_URL]: requestUrl,
336 [RESPONSE_TYPE]: responseType,
337 });

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