Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/common/engine/private_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export { FileLoader as ɵFileLoader, CommonEngine as ɵCommonEngine,
RenderOptions as ɵRenderOptions } from './src/index';
RenderOptions as ɵRenderOptions } from './src/index';
8 changes: 8 additions & 0 deletions modules/common/private_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export { TransferHttpCacheInterceptor as ɵTransferHttpCacheInterceptor } from './src/transfer_http';
2 changes: 1 addition & 1 deletion modules/common/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/
export { TransferHttpCacheModule } from './src/transfer_http';
export { StateTransferInitializerModule } from './src/state-transfer-initializer/module';

export * from './private_api';
40 changes: 40 additions & 0 deletions modules/common/spec/transfer_http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ɵTransferHttpCacheInterceptor as TransferHttpCacheInterceptor } from '@nguniversal/common';
import { of } from 'rxjs';
import { HttpParams } from '@angular/common/http';

function mockAppRef(): any {
return {
isStable: of(true)
};
}

function mockTransferState(): any {
return {
store: {}
};
}

describe('TransferHttp', () => {
describe('keys', () => {
it('should encode url params', () => {
const interceptor = new TransferHttpCacheInterceptor(mockAppRef(), mockTransferState());
const key = interceptor['makeCacheKey']('GET', 'https://google.com/api',
new HttpParams().append('foo', 'bar'));
expect(key).toEqual('G.https://google.com/api?foo=bar');
});
it('should sort the keys by unicode points', () => {
const interceptor = new TransferHttpCacheInterceptor(mockAppRef(), mockTransferState());
const key = interceptor['makeCacheKey']('GET', 'https://google.com/api',
new HttpParams().append('b', 'foo').append('a', 'bar'));
expect(key).toEqual('G.https://google.com/api?a=bar&b=foo');
});
it('should make equal keys if order of params changes', () => {
const interceptor = new TransferHttpCacheInterceptor(mockAppRef(), mockTransferState());
const key1 = interceptor['makeCacheKey']('GET', 'https://google.com/api',
new HttpParams().append('a', 'bar').append('b', 'foo'));
const key2 = interceptor['makeCacheKey']('GET', 'https://google.com/api',
new HttpParams().append('b', 'foo').append('a', 'bar'));
expect(key1).toEqual(key2);
});
});
});
24 changes: 18 additions & 6 deletions modules/common/src/transfer_http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ import {
HttpHeaders,
HttpInterceptor,
HttpRequest,
HttpResponse
HttpResponse,
HttpParams
} from '@angular/common/http';
import {ApplicationRef, Injectable, NgModule} from '@angular/core';
import {BrowserTransferStateModule, TransferState, makeStateKey} from '@angular/platform-browser';
import {
BrowserTransferStateModule,
TransferState,
makeStateKey,
StateKey
} from '@angular/platform-browser';
import {Observable, of as observableOf} from 'rxjs';
import {tap, take, filter} from 'rxjs/operators';

Expand All @@ -41,8 +47,15 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
private isCacheActive = true;

private invalidateCacheEntry(url: string) {
this.transferState.remove(makeStateKey<TransferHttpResponse>('G.' + url));
this.transferState.remove(makeStateKey<TransferHttpResponse>('H.' + url));
Object.keys(this.transferState['store'])
.forEach(key => key.includes(url) ? this.transferState.remove(makeStateKey(key)) : null);
}

private makeCacheKey(method: string, url: string, params: HttpParams): StateKey<string> {
// make the params encoded same as a url so it's easy to identify
const encodedParams = params.keys().sort().map(k => `${k}=${params.get(k)}`).join('&');
const key = (method === 'GET' ? 'G.' : 'H.') + url + '?' + encodedParams;
return makeStateKey<TransferHttpResponse>(key);
}

constructor(appRef: ApplicationRef, private transferState: TransferState) {
Expand All @@ -68,8 +81,7 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
return next.handle(req);
}

const key = (req.method === 'GET' ? 'G.' : 'H.') + req.url;
const storeKey = makeStateKey<TransferHttpResponse>(key);
const storeKey = this.makeCacheKey(req.method, req.url, req.params);

if (this.transferState.hasKey(storeKey)) {
// Request found in cache. Respond using it.
Expand Down