Skip to content

Commit

Permalink
fix(common): handle arrays in querystrings
Browse files Browse the repository at this point in the history
caching GET request with query string with array of parameter gets the first value only.
it breaks caching for the same url with different query string for example
for api http://api.example.com?params=1&params=2&params=3 cache key will be http://api.example.com?params=1
for api http://api.example.com?params=1&params=2 cache key will be the same http://api.example.com?params=1
and therefor API request will not be sent to server.
  • Loading branch information
ansergei committed Aug 7, 2020
1 parent 7a4032a commit b5c2eb2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions modules/common/spec/transfer_http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ describe('TransferHttp', () => {
new HttpParams().append('b', 'foo').append('a', 'bar'));
expect(key1).toEqual(key2);
});
it('should encode arrays in url params', () => {
const interceptor = new TransferHttpCacheInterceptor(mockAppRef(), mockTransferState());
const key = interceptor['makeCacheKey']('GET', 'https://google.com/api',
new HttpParams().append('b', 'xyz').append('a', 'foo').append('a', 'bar'));
expect(key).toEqual('G.https://google.com/api?a=foo,bar&b=xyz');
});
});
});
2 changes: 1 addition & 1 deletion modules/common/src/transfer_http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {

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 encodedParams = params.keys().sort().map(k => `${k}=${params.getAll(k)}`).join('&');
const key = (method === 'GET' ? 'G.' : 'H.') + url + '?' + encodedParams;

return makeStateKey<TransferHttpResponse>(key);
Expand Down

0 comments on commit b5c2eb2

Please sign in to comment.