Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrategyHandler.getCacheKey() should take the request.url into account #2972

Closed
jeffposnick opened this issue Nov 2, 2021 · 0 comments · Fixed by #2973
Closed

StrategyHandler.getCacheKey() should take the request.url into account #2972

jeffposnick opened this issue Nov 2, 2021 · 0 comments · Fixed by #2973
Assignees
Labels
Bug An issue with our existing, production codebase. workbox-strategies

Comments

@jeffposnick
Copy link
Contributor

jeffposnick commented Nov 2, 2021

Library Affected:
workbox-strategies

There's currently some aggressive in-memory caching done inside of the getCacheKey() method of the StrategyHandler class:

async getCacheKey(
request: Request,
mode: 'read' | 'write',
): Promise<Request> {
if (!this._cacheKeys[mode]) {
let effectiveRequest = request;
for (const callback of this.iterateCallbacks('cacheKeyWillBeUsed')) {
effectiveRequest = toRequest(
await callback({
mode,
request: effectiveRequest,
event: this.event,
// params has a type any can't change right now.
params: this.params, // eslint-disable-line
}),
);
}
this._cacheKeys[mode] = effectiveRequest;
}
return this._cacheKeys[mode]!;
}

If this._cacheKeys[mode] is set (where mode is either 'read' or 'write') then the first value it's set to is reused indefinitely, regardless of the value of the request parameter.

This can cause problems if you want to pass in multiple URLs to the same StrategyHandler instance inside of a custom Strategy subclass. This would come up if you, for instance, have a Strategy class that responded to a request for a URL by creating multiple sub-requests, each of whose responses need to be cached independently. As it stands, those sub-requests will each result in the same cache key being used, effectively overwriting the previous response data with a response that doesn't correspond to the URL.

To fix this, we can use a compound key of ${request} | ${mode} for the in-memory cache. Longer-term, we could consider dropping this in-memory cache entirely, but I realize that changing that behavior might require a major semver release.

CC: @philipwalton in case there was a specific motivation for needing this in-memory cache when it was originally implemented.

@jeffposnick jeffposnick added Bug An issue with our existing, production codebase. workbox-strategies labels Nov 2, 2021
@jeffposnick jeffposnick self-assigned this Nov 2, 2021
@jeffposnick jeffposnick changed the title StrategyHandler.getCacheKey() should always recompute the key StrategyHandler.getCacheKey() should take the request.url into account Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An issue with our existing, production codebase. workbox-strategies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant