58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
'use strict';
|
|
const CACHE_NAME = 'hearts-v1';
|
|
|
|
const PRECACHE = [
|
|
'/',
|
|
'/index.html',
|
|
'/style.css',
|
|
'/app.js',
|
|
'/manifest.json',
|
|
'/icons/icon-192.png',
|
|
'/icons/icon-512.png',
|
|
'/icons/icon.svg',
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then(cache => cache.addAll(PRECACHE))
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
|
|
)
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
const url = event.request.url;
|
|
if (event.request.method !== 'GET') return;
|
|
if (url.includes('/socket.io/')) return;
|
|
|
|
if (url.includes('/cards/')) {
|
|
event.respondWith(
|
|
caches.match(event.request).then(cached => {
|
|
if (cached) return cached;
|
|
return fetch(event.request).then(res => {
|
|
caches.open(CACHE_NAME).then(c => c.put(event.request, res.clone()));
|
|
return res;
|
|
});
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then(res => {
|
|
caches.open(CACHE_NAME).then(c => c.put(event.request, res.clone()));
|
|
return res;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
});
|