Add datastore_refresh_lock to prevent concurrent refresh operations. Extract refresh_datastore_storage() function with non-blocking lock acquisition using acquire(blocking=False). Update datastore_snapshot() to spawn background thread for refresh when cache is stale but return immediately with cached data. Add offline fallback HTML page to service worker for navigate requests with 503 status for
26 lines
1.3 KiB
JavaScript
26 lines
1.3 KiB
JavaScript
const CACHE_NAME = 'media-max-shell-v6';
|
|
const SHELL = ['/static/media-mark.svg', '/static/media-mark-192.png', '/static/media-mark-512.png', '/static/manifest.json'];
|
|
|
|
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;
|
|
event.respondWith(fetch(request).catch(() => {
|
|
if (request.mode === 'navigate') {
|
|
return new Response('<!doctype html><meta charset="utf-8"><title>Media Max</title><style>body{margin:0;padding:2rem;background:#181818;color:#ddd;font:16px system-ui}main{max-width:36rem;margin:auto}h1{color:#f4c430}</style><main><h1>Media Max</h1><p>Die Verbindung ist momentan unterbrochen. Bitte erneut laden.</p></main>', {headers: {'Content-Type': 'text/html; charset=utf-8'}});
|
|
}
|
|
return caches.match(request).then(response => response || new Response('', {status: 503}));
|
|
}));
|
|
});
|