fix(web): fall back to network when caches.open fails in service worker

This commit is contained in:
Dylan M. Taylor committed 2026-10-22 19:19:20 -04:00
1 parent 7983396692
commit ec0cc83a29
1 file changed
+8 -6
+8 -6
View File
@@ -32,13 +32,15 @@ self.addEventListener("fetch", (event) => {
if (req.method !== "GET" || url.origin !== self.location.origin) return;
event.respondWith(
(async () => {
const cache = await caches.open(CACHE);
// The page itself regardless of its query (?empty, ?demo…).
const key = req.mode === "navigate" ? new URL("./", self.registration.scope).href : req;
const hit = await cache.match(key, { ignoreSearch: req.mode === "navigate" });
if (hit) return hit;
const cache = await caches.open(CACHE).catch(() => null);
if (cache) {
// The page itself regardless of its query (?empty, ?demo…).
const key = req.mode === "navigate" ? new URL("./", self.registration.scope).href : req;
const hit = await cache.match(key, { ignoreSearch: req.mode === "navigate" }).catch(() => null);
if (hit) return hit;
}
const res = await fetch(req);
if (res.ok && res.type === "basic") void cache.put(req, res.clone()).catch(() => {});
if (cache && res.ok && res.type === "basic") void cache.put(req, res.clone()).catch(() => {});
return res;
})(),
);