@@ -114,7 +114,7 @@ interface CacheOptions extends HttpTransferCacheOptions {
114114 isCacheActive : boolean ;
115115}
116116
117- const CACHE_OPTIONS = new InjectionToken < CacheOptions > (
117+ export const CACHE_OPTIONS = new InjectionToken < CacheOptions > (
118118 typeof ngDevMode !== 'undefined' && ngDevMode ? 'HTTP_TRANSFER_STATE_CACHE_OPTIONS' : '' ,
119119) ;
120120
@@ -123,11 +123,13 @@ const CACHE_OPTIONS = new InjectionToken<CacheOptions>(
123123 */
124124const ALLOWED_METHODS = [ 'GET' , 'HEAD' ] ;
125125
126- export function transferCacheInterceptorFn (
126+ export function StateFromCache (
127127 req : HttpRequest < unknown > ,
128- next : HttpHandlerFn ,
129- ) : Observable < HttpEvent < unknown > > {
130- const { isCacheActive, ...globalOptions } = inject ( CACHE_OPTIONS ) ;
128+ options : CacheOptions ,
129+ transferState : TransferState ,
130+ originMap : Record < string , string > | null ,
131+ ) : HttpResponse < unknown > | null {
132+ const { isCacheActive, ...globalOptions } = options ;
131133 const { transferCache : requestOptions , method : requestMethod } = req ;
132134
133135 // In the following situations we do not want to cache the request
@@ -141,15 +143,9 @@ export function transferCacheInterceptorFn(
141143 ( ! globalOptions . includeRequestsWithAuthHeaders && hasAuthHeaders ( req ) ) ||
142144 globalOptions . filter ?.( req ) === false
143145 ) {
144- return next ( req ) ;
146+ return null ;
145147 }
146148
147- const transferState = inject ( TransferState ) ;
148-
149- const originMap : Record < string , string > | null = inject ( HTTP_TRANSFER_CACHE_ORIGIN_MAP , {
150- optional : true ,
151- } ) ;
152-
153149 if ( typeof ngServerMode !== 'undefined' && ! ngServerMode && originMap ) {
154150 throw new RuntimeError (
155151 RuntimeErrorCode . HTTP_ORIGIN_MAP_USED_IN_CLIENT ,
@@ -206,15 +202,59 @@ export function transferCacheInterceptorFn(
206202 headers = appendMissingHeadersDetection ( req . url , headers , headersToInclude ?? [ ] ) ;
207203 }
208204
209- return of (
210- new HttpResponse ( {
211- body,
212- headers,
213- status,
214- statusText,
215- url,
216- } ) ,
217- ) ;
205+ return new HttpResponse ( {
206+ body,
207+ headers,
208+ status,
209+ statusText,
210+ url,
211+ } ) ;
212+ }
213+
214+ return null ;
215+ }
216+
217+ export function transferCacheInterceptorFn (
218+ req : HttpRequest < unknown > ,
219+ next : HttpHandlerFn ,
220+ ) : Observable < HttpEvent < unknown > > {
221+ const options = inject ( CACHE_OPTIONS ) ;
222+ const transferState = inject ( TransferState ) ;
223+
224+ const originMap : Record < string , string > | null = inject ( HTTP_TRANSFER_CACHE_ORIGIN_MAP , {
225+ optional : true ,
226+ } ) ;
227+
228+ const cachedResponse = StateFromCache ( req , options , transferState , originMap ) ;
229+ if ( cachedResponse ) {
230+ return of ( cachedResponse ) ;
231+ }
232+
233+ const { isCacheActive, ...globalOptions } = options ;
234+ const { transferCache : requestOptions , method : requestMethod } = req ;
235+ let headersToInclude = globalOptions . includeHeaders ;
236+ if ( typeof requestOptions === 'object' && requestOptions . includeHeaders ) {
237+ headersToInclude = requestOptions . includeHeaders ;
238+ }
239+
240+ const requestUrl =
241+ typeof ngServerMode !== 'undefined' && ngServerMode && originMap
242+ ? mapRequestOriginUrl ( req . url , originMap )
243+ : req . url ;
244+ const storeKey = makeCacheKey ( req , requestUrl ) ;
245+
246+ // In the following situations we do not want to cache the request
247+ if (
248+ ! isCacheActive ||
249+ requestOptions === false ||
250+ // POST requests are allowed either globally or at request level
251+ ( requestMethod === 'POST' && ! globalOptions . includePostRequests && ! requestOptions ) ||
252+ ( requestMethod !== 'POST' && ! ALLOWED_METHODS . includes ( requestMethod ) ) ||
253+ // Do not cache request that require authorization when includeRequestsWithAuthHeaders is falsey
254+ ( ! globalOptions . includeRequestsWithAuthHeaders && hasAuthHeaders ( req ) ) ||
255+ globalOptions . filter ?.( req ) === false
256+ ) {
257+ return next ( req ) ;
218258 }
219259
220260 const event$ = next ( req ) ;
0 commit comments