Skip to content

Commit

Permalink
Make request.url part of the key (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Nov 3, 2021
1 parent cdad230 commit bbaac60
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/workbox-strategies/src/StrategyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class StrategyHandler {
public event: ExtendableEvent;
public params?: any;

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

private readonly _strategy: Strategy;
private readonly _extendLifetimePromises: Promise<any>[];
Expand Down Expand Up @@ -437,7 +437,8 @@ class StrategyHandler {
request: Request,
mode: 'read' | 'write',
): Promise<Request> {
if (!this._cacheKeys[mode]) {
const key = `${request.url} | ${mode}`;
if (!this._cacheKeys[key]) {
let effectiveRequest = request;

for (const callback of this.iterateCallbacks('cacheKeyWillBeUsed')) {
Expand All @@ -452,9 +453,9 @@ class StrategyHandler {
);
}

this._cacheKeys[mode] = effectiveRequest;
this._cacheKeys[key] = effectiveRequest;
}
return this._cacheKeys[mode]!;
return this._cacheKeys[key];
}

/**
Expand Down
12 changes: 11 additions & 1 deletion test/workbox-strategies/sw/test-StrategyHandler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ describe(`StrategyHandler`, function () {
expect(writeCacheKey.url).to.equal(location.origin + '/test+1+write');
});

it(`caches the key per mode to avoid repeat invocations`, async function () {
it(`caches the key using request + mode avoid repeat invocations`, async function () {
const request = new Request('/test');
const plugin = {
cacheKeyWillBeUsed({mode, request}) {
Expand Down Expand Up @@ -1323,6 +1323,16 @@ describe(`StrategyHandler`, function () {
await handler.getCacheKey(request, 'write');
await handler.getCacheKey(request, 'write');
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(2);

// See https://github.com/GoogleChrome/workbox/issues/2972
const secondRequest = new Request('/test2');

await handler.getCacheKey(secondRequest, 'read');
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(3);

await handler.getCacheKey(secondRequest, 'write');
await handler.getCacheKey(secondRequest, 'write');
expect(plugin.cacheKeyWillBeUsed.callCount).to.equal(4);
});
});
});

0 comments on commit bbaac60

Please sign in to comment.