@@ -12,10 +12,16 @@ import {
1212 HttpHeaders ,
1313 HttpInterceptor ,
1414 HttpRequest ,
15- HttpResponse
15+ HttpResponse ,
16+ HttpParams
1617} from '@angular/common/http' ;
1718import { ApplicationRef , Injectable , NgModule } from '@angular/core' ;
18- import { BrowserTransferStateModule , TransferState , makeStateKey } from '@angular/platform-browser' ;
19+ import {
20+ BrowserTransferStateModule ,
21+ TransferState ,
22+ makeStateKey ,
23+ StateKey
24+ } from '@angular/platform-browser' ;
1925import { Observable , of as observableOf } from 'rxjs' ;
2026import { tap , take , filter } from 'rxjs/operators' ;
2127
@@ -41,8 +47,15 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
4147 private isCacheActive = true ;
4248
4349 private invalidateCacheEntry ( url : string ) {
44- this . transferState . remove ( makeStateKey < TransferHttpResponse > ( 'G.' + url ) ) ;
45- this . transferState . remove ( makeStateKey < TransferHttpResponse > ( 'H.' + url ) ) ;
50+ Object . keys ( this . transferState [ 'store' ] )
51+ . forEach ( key => key . includes ( url ) ? this . transferState . remove ( makeStateKey ( key ) ) : null ) ;
52+ }
53+
54+ private makeCacheKey ( method : string , url : string , params : HttpParams ) : StateKey < string > {
55+ // make the params encoded same as a url so it's easy to identify
56+ const encodedParams = params . keys ( ) . sort ( ) . map ( k => `${ k } =${ params . get ( k ) } ` ) . join ( '&' ) ;
57+ const key = ( method === 'GET' ? 'G.' : 'H.' ) + url + '?' + encodedParams ;
58+ return makeStateKey < TransferHttpResponse > ( key ) ;
4659 }
4760
4861 constructor ( appRef : ApplicationRef , private transferState : TransferState ) {
@@ -68,8 +81,7 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
6881 return next . handle ( req ) ;
6982 }
7083
71- const key = ( req . method === 'GET' ? 'G.' : 'H.' ) + req . url ;
72- const storeKey = makeStateKey < TransferHttpResponse > ( key ) ;
84+ const storeKey = this . makeCacheKey ( req . method , req . url , req . params ) ;
7385
7486 if ( this . transferState . hasKey ( storeKey ) ) {
7587 // Request found in cache. Respond using it.
0 commit comments