Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit b5c2eb2

Browse files
authored
fix(common): handle arrays in querystrings
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.
1 parent 7a4032a commit b5c2eb2

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

modules/common/spec/transfer_http.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@ describe('TransferHttp', () => {
3636
new HttpParams().append('b', 'foo').append('a', 'bar'));
3737
expect(key1).toEqual(key2);
3838
});
39+
it('should encode arrays in url params', () => {
40+
const interceptor = new TransferHttpCacheInterceptor(mockAppRef(), mockTransferState());
41+
const key = interceptor['makeCacheKey']('GET', 'https://google.com/api',
42+
new HttpParams().append('b', 'xyz').append('a', 'foo').append('a', 'bar'));
43+
expect(key).toEqual('G.https://google.com/api?a=foo,bar&b=xyz');
44+
});
3945
});
4046
});

modules/common/src/transfer_http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class TransferHttpCacheInterceptor implements HttpInterceptor {
5454

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

6060
return makeStateKey<TransferHttpResponse>(key);

0 commit comments

Comments
 (0)