Files

39 lines
1.1 KiB
JavaScript

const CACHE_NAME = 'calendar-reminder-cache-v3';
const ASSETS = [
'/WebApp/index.html',
'/WebApp/css/styles.css',
'/WebApp/css/bootstrap.local.css',
'/WebApp/js/config.js',
'/WebApp/js/db.js',
'/WebApp/js/models.js',
'/WebApp/js/ui.js',
'/WebApp/js/main.js',
'/WebApp/js/singleInstance.js',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)).catch(() => null)
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))))
);
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return;
event.respondWith(
caches.match(req).then((cached) => {
const fetchPromise = fetch(req).then((resp) => {
const copy = resp.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(req, copy)).catch(() => {});
return resp;
}).catch(() => cached);
return cached || fetchPromise;
})
);
});