27 lines
1.1 KiB
JavaScript
27 lines
1.1 KiB
JavaScript
const CACHE_NAME = 'media-max-shell-v9';
|
|
const SHELL = ['/static/media-mark.svg', '/static/media-mark-192.png', '/static/media-mark-512.png', '/static/manifest.json', '/static/dashboard.css?v=9', '/static/dashboard.js?v=8'];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(SHELL)));
|
|
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 => {
|
|
const request = event.request;
|
|
if (request.method !== 'GET') return;
|
|
const url = new URL(request.url);
|
|
if (url.pathname.startsWith('/api/')) return;
|
|
// Navigation requests must stay real network requests. Returning an
|
|
// offline document here can mask backend errors and make the app appear
|
|
// disconnected after a scan/reload.
|
|
if (request.mode === 'navigate') return;
|
|
event.respondWith(fetch(request).catch(() => {
|
|
return caches.match(request).then(response => response || new Response('', {status: 503}));
|
|
}));
|
|
});
|