Skip to content

Commit bbaac60

Browse files
authored
Make request.url part of the key (#2973)
1 parent cdad230 commit bbaac60

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

packages/workbox-strategies/src/StrategyHandler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class StrategyHandler {
4343
public event: ExtendableEvent;
4444
public params?: any;
4545

46-
private _cacheKeys: {read?: Request; write?: Request} = {};
46+
private _cacheKeys: Record<string, Request> = {};
4747

4848
private readonly _strategy: Strategy;
4949
private readonly _extendLifetimePromises: Promise<any>[];
@@ -437,7 +437,8 @@ class StrategyHandler {
437437
request: Request,
438438
mode: 'read' | 'write',
439439
): Promise<Request> {
440-
if (!this._cacheKeys[mode]) {
440+
const key = `${request.url} | ${mode}`;
441+
if (!this._cacheKeys[key]) {
441442
let effectiveRequest = request;
442443

443444
for (const callback of this.iterateCallbacks('cacheKeyWillBeUsed')) {
@@ -452,9 +453,9 @@ class StrategyHandler {
452453
);
453454
}
454455

455-
this._cacheKeys[mode] = effectiveRequest;
456+
this._cacheKeys[key] = effectiveRequest;
456457
}
457-
return this._cacheKeys[mode]!;
458+
return this._cacheKeys[key];
458459
}
459460

460461
/**

test/workbox-strategies/sw/test-StrategyHandler.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ describe(`StrategyHandler`, function () {
12951295
expect(writeCacheKey.url).to.equal(location.origin + '/test+1+write');
12961296
});
12971297

1298-
it(`caches the key per mode to avoid repeat invocations`, async function () {
1298+
it(`caches the key using request + mode avoid repeat invocations`, async function () {
12991299
const request = new Request('/test');
13001300
const plugin = {
13011301
cacheKeyWillBeUsed({mode, request}) {
@@ -1323,6 +1323,16 @@ describe(`StrategyHandler`, function () {
13231323
await handler.getCacheKey(request, 'write');
13241324
await handler.getCacheKey(request, 'write');
13251325
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(2);
1326+
1327+
// See https://github.com/GoogleChrome/workbox/issues/2972
1328+
const secondRequest = new Request('/test2');
1329+
1330+
await handler.getCacheKey(secondRequest, 'read');
1331+
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(3);
1332+
1333+
await handler.getCacheKey(secondRequest, 'write');
1334+
await handler.getCacheKey(secondRequest, 'write');
1335+
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(4);
13261336
});
13271337
});
13281338
});

0 commit comments

Comments
 (0)