1const CACHE_NAME = "sevisa-cache-v1"; 2const OFFLINE_URL = "/offline.html"; 3 4// Pre-cache important assets 5const PRECACHE_ASSETS = [ 6 "/", 7 "/offline.html", 8 "/icons/icon-192x192.png", 9 "/icons/icon-256x256.png", 10 "/icons/icon-384x384.png", 11 "/icons/icon-512x512.png" 12]; 13 14// Install service worker 15self.addEventListener("install", (event) => { 16 // console.log("ð¦ Service Worker: Installing"); 17 18 event.waitUntil( 19 caches.open(CACHE_NAME).then((cache) => { 20 // console.log("ð¦ Caching precache assets"); 21 return cache.addAll(PRECACHE_ASSETS); 22 }) 23 ); 24 25 self.skipWaiting(); 26}); 27 28// Activate service worker 29self.addEventListener("activate", (event) => { 30 // console.log("â¡ Service Worker: Activated"); 31 32 event.waitUntil( 33 caches.keys().then((keys) => { 34 return Promise.all( 35 keys 36 .filter((key) => key !== CACHE_NAME) 37 .map((key) => caches.delete(key)) 38 ); 39 }) 40 ); 41 42 self.clients.claim(); 43}); 44 45// Fetch handler (Network First for HTML, Cache First for others) 46self.addEventListener("fetch", (event) => { 47 if (event.request.method !== "GET") return; 48 49 const req = event.request; 50 51 // === Skip chrome-extension:// and other unsupported schemes === 52 if (!req.url.startsWith("http")) { 53 return; // do NOT try to cache these requests 54 } 55 56 // Network-first strategy for HTML pages 57 if (req.headers.get("accept")?.includes("text/html")) { 58 event.respondWith( 59 fetch(req) 60 .then((response) => { 61 // Cache the updated page in background 62 const copy = response.clone(); 63 caches.open(CACHE_NAME).then((cache) => { 64 cache.put(req, copy); 65 }); 66 return response; 67 }) 68 .catch(() => caches.match(req).then((res) => res || caches.match(OFFLINE_URL))) 69 ); 70 return; 71 } 72 73 // Cache-first strategy for static files 74 event.respondWith( 75 caches.match(req).then((cacheRes) => { 76 return ( 77 cacheRes || 78 fetch(req).then((networkRes) => { 79 if (!networkRes || networkRes.status !== 200) return networkRes; 80 81 const copy = networkRes.clone(); 82 // caches.open(CACHE_NAME).then((cache) => cache.put(req, copy)); 83 // Skip caching anything not HTTP 84 if (req.url.startsWith("http")) { 85 caches.open(CACHE_NAME).then((cache) => cache.put(req, copy)); 86 } 87 88 return networkRes; 89 }) 90 ); 91 }) 92 ); 93});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.