23 lines
848 B
JavaScript
23 lines
848 B
JavaScript
|
|
const CACHE = 'posimai-store-v1';
|
||
|
|
const STATIC = ['/', '/index.html', '/manifest.json', '/logo.png'];
|
||
|
|
|
||
|
|
self.addEventListener('install', e => {
|
||
|
|
e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)).then(() => self.skipWaiting()));
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('activate', e => {
|
||
|
|
e.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))).then(() => self.clients.claim()));
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('fetch', e => {
|
||
|
|
if (e.request.method !== 'GET') return;
|
||
|
|
if (new URL(e.request.url).origin !== location.origin) return;
|
||
|
|
e.respondWith(
|
||
|
|
caches.match(e.request).then(cached => cached || fetch(e.request).then(res => {
|
||
|
|
const clone = res.clone();
|
||
|
|
caches.open(CACHE).then(c => c.put(e.request, clone));
|
||
|
|
return res;
|
||
|
|
}))
|
||
|
|
);
|
||
|
|
});
|