Files
Life-OS/public/service-worker.js
T

32 lines
1.3 KiB
JavaScript

const CACHE = 'life-os-shell-v1.13.0';
const SHELL = [
'/', '/index.html', '/manifest.webmanifest', '/life-os-icon.svg'
];
self.addEventListener('install', event => {
event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(SHELL)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', event => {
event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key.startsWith('life-os-shell-') && key !== CACHE).map(key => caches.delete(key)))).then(() => self.clients.claim()));
});
self.addEventListener('fetch', event => {
const request = event.request;
if (request.method !== 'GET' || new URL(request.url).origin !== self.location.origin) return;
const url = new URL(request.url);
if (url.pathname.startsWith('/api/')) return;
if (request.mode === 'navigate') {
event.respondWith(fetch(request).then(response => {
const copy = response.clone();
caches.open(CACHE).then(cache => cache.put('/index.html', copy));
return response;
}).catch(() => caches.match('/index.html')));
return;
}
event.respondWith(caches.match(request).then(cached => cached || fetch(request).then(response => {
if (response.ok) caches.open(CACHE).then(cache => cache.put(request, response.clone()));
return response;
})));
});