fix(service-worker): freshness strategy should clone response for cache (#19764)
When Cache.put() is called with a Response, it consumes the response. If the Response is used for any other purpose (such as satisfying the original FetchEvent) it must be cloned first. A bug exists in the mocks used for SW tests, where this condition is not validated. The bodies of MockResponses can be utilized repeatedly without erroring in the same way that a real browser would. This bug is fixed by this commit, which causes tests for the freshness strategy of data caching to start failing. The cause of this failure is a second bug in the data caching code, where the Response is not cloned prior to being passed to Cache.put(). This is also fixed. PR Close #19764
This commit is contained in:

committed by
Tobias Bosch

parent
fcfb1544e8
commit
396c2417d9
@ -370,8 +370,7 @@ export class DataGroup {
|
||||
}
|
||||
|
||||
// The request completed in time, so cache it inline with the response flow.
|
||||
// Make sure to clone it so the real response can still be returned to the user.
|
||||
await this.cacheResponse(req, res.clone(), lru);
|
||||
await this.cacheResponse(req, res, lru);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -409,7 +408,7 @@ export class DataGroup {
|
||||
|
||||
// No response in the cache. No choice but to fall back on the full network fetch.
|
||||
res = await networkFetch;
|
||||
await this.cacheResponse(req, res.clone(), lru, true);
|
||||
await this.cacheResponse(req, res, lru, true);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -517,8 +516,9 @@ export class DataGroup {
|
||||
// until enough other resources are requested that it falls off the end of the LRU chain.
|
||||
lru.accessed(req.url);
|
||||
|
||||
// Store the response in the cache.
|
||||
await(await this.cache).put(req, res);
|
||||
// Store the response in the cache (cloning because the browser will consume
|
||||
// the body during the caching operation).
|
||||
await(await this.cache).put(req, res.clone());
|
||||
|
||||
// Store the age of the cache.
|
||||
const ageTable = await this.ageTable;
|
||||
|
Reference in New Issue
Block a user