v0.14.22
This commit is contained in:
+24
-4
@@ -6,8 +6,28 @@ self.addEventListener('install', event => event.waitUntil(caches.open(CACHE).the
|
||||
self.addEventListener('activate', event => event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key)))).then(() => self.clients.claim())));
|
||||
self.addEventListener('fetch', event => {
|
||||
const url = new URL(event.request.url);
|
||||
if (event.request.method !== 'GET' || url.pathname.startsWith(path('/api/')) || url.pathname.startsWith(path('/lang/')) || event.request.mode === 'navigate') return;
|
||||
event.respondWith(caches.match(event.request).then(cached => cached || fetch(event.request).then(response => {
|
||||
const copy = response.clone(); caches.open(CACHE).then(cache => cache.put(event.request, copy)); return response;
|
||||
})));
|
||||
if (
|
||||
event.request.method !== 'GET'
|
||||
|| (url.protocol !== 'http:' && url.protocol !== 'https:')
|
||||
|| url.origin !== self.location.origin
|
||||
|| url.pathname.startsWith(path('/api/'))
|
||||
|| url.pathname.startsWith(path('/lang/'))
|
||||
|| event.request.mode === 'navigate'
|
||||
) return;
|
||||
|
||||
event.respondWith((async () => {
|
||||
const cached = await caches.match(event.request);
|
||||
if (cached) return cached;
|
||||
|
||||
const response = await fetch(event.request);
|
||||
if (response.ok && response.type === 'basic') {
|
||||
try {
|
||||
const cache = await caches.open(CACHE);
|
||||
await cache.put(event.request, response.clone());
|
||||
} catch (_) {
|
||||
// Cache failures must not break the network response.
|
||||
}
|
||||
}
|
||||
return response;
|
||||
})());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user