refactor(service-worker): make signatures of caching methods compatible (#32996)

Make safe caching and unsafe caching methods compatible so they can be
swapped. Gives more flexibility when writing http response processing
code.

PR Close #32996
This commit is contained in:
Denis Omelkov 2019-10-14 13:48:38 +05:00 committed by Miško Hevery
parent b04488d692
commit 1353afc2b1
1 changed files with 18 additions and 7 deletions

View File

@ -334,7 +334,7 @@ export class DataGroup {
res = fromCache.res;
// Check the age of the resource.
if (this.config.refreshAheadMs !== undefined && fromCache.age >= this.config.refreshAheadMs) {
ctx.waitUntil(this.safeCacheResponse(req, this.safeFetch(req)));
ctx.waitUntil(this.safeCacheResponse(req, this.safeFetch(req), lru));
}
}
@ -353,7 +353,7 @@ export class DataGroup {
res = this.adapter.newResponse(null, {status: 504, statusText: 'Gateway Timeout'});
// Cache the network response eventually.
ctx.waitUntil(this.safeCacheResponse(req, networkFetch));
ctx.waitUntil(this.safeCacheResponse(req, networkFetch, lru));
} else {
// The request completed in time, so cache it inline with the response flow.
await this.cacheResponse(req, res, lru);
@ -378,7 +378,7 @@ export class DataGroup {
// If the network fetch times out or errors, fall back on the cache.
if (res === undefined) {
ctx.waitUntil(this.safeCacheResponse(req, networkFetch, true));
ctx.waitUntil(this.safeCacheResponse(req, networkFetch, lru, true));
// Ignore the age, the network response will be cached anyway due to the
// behavior of freshness.
@ -432,12 +432,23 @@ export class DataGroup {
}
}
private async safeCacheResponse(req: Request, res: Promise<Response>, okToCacheOpaque?: boolean):
Promise<void> {
private async safeCacheResponse(
req: Request, resOrPromise: Promise<Response>|Response, lru: LruList,
okToCacheOpaque?: boolean): Promise<void> {
try {
await this.cacheResponse(req, await res, await this.lru(), okToCacheOpaque);
const res = await resOrPromise;
try {
await this.cacheResponse(req, res, lru, okToCacheOpaque);
} catch (err) {
// Saving the API response failed. This could be a result of a full storage.
// Since this data is cached lazily and temporarily, continue serving clients as usual.
// TODO: Log error
// TODO: Better detect/handle full storage; e.g. using
// [navigator.storage](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorStorage/storage).
}
} catch {
// TODO: handle this error somehow?
// Request failed
// TODO: Handle this error somehow?
}
}