From 1353afc2b1155470a9402b48e9a8d185b7f6df5e Mon Sep 17 00:00:00 2001 From: Denis Omelkov Date: Mon, 14 Oct 2019 13:48:38 +0500 Subject: [PATCH] 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 --- packages/service-worker/worker/src/data.ts | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/service-worker/worker/src/data.ts b/packages/service-worker/worker/src/data.ts index 8850877ed8..9ba08f029f 100644 --- a/packages/service-worker/worker/src/data.ts +++ b/packages/service-worker/worker/src/data.ts @@ -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, okToCacheOpaque?: boolean): - Promise { + private async safeCacheResponse( + req: Request, resOrPromise: Promise|Response, lru: LruList, + okToCacheOpaque?: boolean): Promise { 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? } }