const CACHE_NAME = 'cidr-calc-v1'; const ASSETS = [ './', './index.html', './style.css', './app.js', './manifest.json', './icons/icon-192.png', './icons/icon-512.png' ]; self.addEventListener('install', (e) => { e.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(ASSETS); }).then(() => self.skipWaiting()) ); }); self.addEventListener('activate', (e) => { e.waitUntil( caches.keys().then((keys) => { return Promise.all( keys.map((key) => { if (key !== CACHE_NAME) { return caches.delete(key); } }) ); }).then(() => self.clients.claim()) ); }); self.addEventListener('fetch', (e) => { e.respondWith( caches.match(e.request).then((cachedResponse) => { if (cachedResponse) { // Fetch fresh in background and update cache fetch(e.request) .then((networkResponse) => { if (networkResponse.status === 200) { caches.open(CACHE_NAME).then((cache) => cache.put(e.request, networkResponse)); } }) .catch(() => {/* Ignore network failures, use cached */}); return cachedResponse; } return fetch(e.request).then((networkResponse) => { if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') { return networkResponse; } const responseToCache = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(e.request, responseToCache); }); return networkResponse; }); }) ); });