29 lines
815 B
JavaScript
29 lines
815 B
JavaScript
const CACHE = 'posimai-veil-v1';
|
||
const ASSETS = ['/', '/index.html', '/manifest.json'];
|
||
const ORIGIN = self.location.origin;
|
||
|
||
self.addEventListener('install', e => {
|
||
e.waitUntil(
|
||
caches.open(CACHE).then(c => c.addAll(ASSETS))
|
||
);
|
||
self.skipWaiting();
|
||
});
|
||
|
||
self.addEventListener('activate', e => {
|
||
e.waitUntil(
|
||
caches.keys().then(keys =>
|
||
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
||
)
|
||
);
|
||
self.clients.claim();
|
||
});
|
||
|
||
self.addEventListener('fetch', e => {
|
||
// クロスオリジンリクエストはキャッシュしない(API等が古いデータを返すのを防ぐ)
|
||
if (!e.request.url.startsWith(ORIGIN)) return;
|
||
|
||
e.respondWith(
|
||
caches.match(e.request).then(cached => cached || fetch(e.request))
|
||
);
|
||
});
|