17 lines
1.1 KiB
JavaScript
17 lines
1.1 KiB
JavaScript
// Офлайн-кэш только публичных ресурсов; личные страницы и ответы требуют сети.
|
|
const CACHE = 'tpt-v4-private-profile-dish-card';
|
|
const FILES = ['gym-plan.i18n.js', 'manifest.webmanifest', 'icon.svg'];
|
|
self.addEventListener('install', e => { e.waitUntil(caches.open(CACHE).then(c => c.addAll(FILES)).then(() => self.skipWaiting())); });
|
|
self.addEventListener('activate', e => {
|
|
e.waitUntil(caches.keys().then(ks => Promise.all(ks.filter(k => k !== CACHE).map(k => caches.delete(k)))).then(() => self.clients.claim()));
|
|
});
|
|
self.addEventListener('fetch', e => {
|
|
if (e.request.method !== 'GET' || !FILES.some(f => new URL(e.request.url).pathname === '/' + f)) return;
|
|
e.respondWith(fetch(e.request).then(r => {
|
|
const copy = r.clone();
|
|
caches.open(CACHE).then(c => c.put(e.request, copy));
|
|
return r;
|
|
}).catch(() => caches.match(e.request).then(m => m || Response.error())));
|
|
});
|
|
self.addEventListener('notificationclick', e => { e.notification.close(); e.waitUntil(self.clients.openWindow('./')); });
|