sw.js: ignore non-http(s) and non-GET requests in fetch handler

The fetch handler tried to cache every intercepted request, so a
chrome-extension:// request made cache.put() throw 'Request scheme
chrome-extension is unsupported'. Skip non-http(s) and non-GET requests,
and swallow cache-write failures.
This commit is contained in:
catarrh committed 2026-08-15 00:40:26 +03:00
1 parent 9362bef869
commit d32da2b375
1 file changed
+3 -1
+3 -1
View File
@@ -33,11 +33,13 @@ self.addEventListener('activate', e => {
self.addEventListener('fetch', e => {
// Network-first: the local server is always up, so always serve fresh
// content and fall back to cache only when offline.
if (!e.request.url.startsWith('http')) return;
if (e.request.method !== 'GET') return;
e.respondWith(
fetch(e.request)
.then(r => {
const clone = r.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => {});
return r;
})
.catch(() => caches.match(e.request))