51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
const CACHE_NAME = 'seoul-shortcuts-v1';
|
|
const CORE_ASSETS = [
|
|
'./',
|
|
'./assets/favicon.svg',
|
|
'./assets/icon-32.png',
|
|
'./assets/icon-192.png',
|
|
'./assets/icon-512.png',
|
|
'./assets/apple-touch-icon.png',
|
|
'./manifest.webmanifest'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.addAll(CORE_ASSETS))
|
|
.catch(() => undefined)
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys => Promise.all(
|
|
keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
|
|
))
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
if (event.request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
if (url.origin !== location.origin) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then(response => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE_NAME).then(cache => cache.put(event.request, copy));
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
});
|