From dcaad169ec8bf0a61d032ae1ae68fb90d1face09 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Sun, 24 Sep 2023 23:37:45 +0200 Subject: [PATCH] fix(service-worker): throw a critical error when `handleFetch` fails (#51885) On Safari, the cache might fail on methods like `match` with an `Internal error`. Critical errors allows to fallback to `safeFetch()` in the `Driver`. fixes: #50378 PR Close #51885 --- packages/service-worker/worker/src/assets.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/service-worker/worker/src/assets.ts b/packages/service-worker/worker/src/assets.ts index c82a623596d..d762ec3dc96 100644 --- a/packages/service-worker/worker/src/assets.ts +++ b/packages/service-worker/worker/src/assets.ts @@ -128,7 +128,17 @@ export abstract class AssetGroup { // Look for a cached response. If one exists, it can be used to resolve the fetch // operation. - const cachedResponse = await cache.match(req, this.config.cacheQueryOptions); + let cachedResponse: Response|undefined; + try { + // Safari 16.4/17 is known to sometimes throw an unexpected internal error on cache access + // This try/catch is here as a workaround to prevent a failure of the handleFetch + // as the Driver falls back to safeFetch on critical errors. + // See #50378 + cachedResponse = await cache.match(req, this.config.cacheQueryOptions); + } catch (error) { + throw new SwCriticalError(`Cache is throwing while looking for a match: ${error}`); + } + if (cachedResponse !== undefined) { // A response has already been cached (which presumably matches the hash for this // resource). Check whether it's safe to serve this resource from cache.