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

Function transferCacheInterceptorFn

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

Source from the content-addressed store, hash-verified

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

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