"use strict"; const debugLog = false; // Establish a cache name const cacheName = "dandelion_cache_v1"; const contentToCache = [ '/static/index.html', '/static/main.css', '/static/main2.js', '/static/lib/marked.min.js', '/static/lib/qrcode.min.js', '/static/db.js', '/static/PeerManager.js', '/static/IDUtils.js', '/static/dataUtils.js', '/static/App.js', '/static/Sync.js', '/static/IDUtils.js', '/static/log.js', '/static/favicon.ico' ]; self.addEventListener("install", (e) => { e.waitUntil((async () => { const cache = await caches.open(cacheName); console.log("[Service Worker] Caching all: app shell and content", contentToCache); try { await cache.addAll(contentToCache); } catch (e) { debugLog ? console.log(e) : null; } })()); }); async function fetchAndUpdateCache(request) { debugLog && console.log('Service Worker: Fetching', request.url); let networkResponse; try { networkResponse = await fetch(request); } catch (e) { debugLog && console.log('Service Worker: Failed to fetch', e); return new Response('Network error occurred', { status: 404, statusText: 'Cache miss and fetch failed', headers: { 'Content-Type': 'text/plain' } }); } debugLog && console.log('Service Worker: Updating cache', request.url); const cache = await caches.open(cacheName); try { await cache.put(request, networkResponse.clone()); } catch (e) { debugLog && console.log('Service Worker: failed to update cache', request.url, e); } return networkResponse; } self.addEventListener('fetch', function (event) { const networkFetch = fetchAndUpdateCache(event.request); event.respondWith(async function () { const cache = await caches.open(cacheName); const cached = await cache.match(event.request); if (cached) { debugLog && console.log('Service Worker: Cache hit', event.request.url); return cached; } return networkFetch; }()); // Keep the SW alive until the background cache update finishes event.waitUntil(networkFetch); }); addEventListener("message", async (e) => { debugLog ? console.log(`Message received:`, e.data) : null; switch (e.data.type) { case "update_app": const cache = await caches.open(cacheName); debugLog ? console.log(`[Service Worker] Caching resources`) : null; // cache.put("/main.js", new Response()); for (let item of contentToCache) { cache.delete(item); } await cache.addAll(contentToCache); break; } }); //# sourceMappingURL=sw.js.map