25 lines
877 B
JavaScript
25 lines
877 B
JavaScript
const CACHE = 'director-v1';
|
|
const SHELL = ['/', '/index.html', '/src/main.js', '/src/styles.css'];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).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;
|
|
e.respondWith(
|
|
caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => {
|
|
if (res.ok && res.type === 'basic') {
|
|
const copy = res.clone();
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.put(e.request, copy)));
|
|
}
|
|
return res;
|
|
}).catch(() => caches.match('/index.html')))
|
|
);
|
|
});
|