1const CACHE_NAME = 'snappilot-v2'; 2const RUNTIME_CACHE = 'snappilot-runtime-v2'; 3 4// Assets to cache on install 5const PRECACHE_ASSETS = [ 6 '/', 7 '/index.html', 8]; 9 10// Install event - precache critical assets 11self.addEventListener('install', (event) => { 12 event.waitUntil( 13 caches.open(CACHE_NAME) 14 .then((cache) => cache.addAll(PRECACHE_ASSETS)) 15 .then(() => self.skipWaiting()) 16 ); 17}); 18 19// Activate event - clean up old caches 20self.addEventListener('activate', (event) => { 21 event.waitUntil( 22 caches.keys().then((cacheNames) => { 23 return Promise.all( 24 cacheNames 25 .filter((name) => name !== CACHE_NAME && name !== RUNTIME_CACHE) 26 .map((name) => caches.delete(name)) 27 ); 28 }).then(() => self.clients.claim()) 29 ); 30}); 31 32// Fetch event - network first, fallback to cache for navigation 33self.addEventListener('fetch', (event) => { 34 const { request } = event; 35 const url = new URL(request.url); 36 37 // Skip cross-origin requests 38 if (url.origin !== location.origin) return; 39 40 // For HTML requests, use network-first strategy 41 if (request.mode === 'navigate') { 42 event.respondWith( 43 fetch(request) 44 .then((response) => { 45 const responseClone = response.clone(); 46 caches.open(RUNTIME_CACHE).then((cache) => { 47 cache.put(request, responseClone); 48 }); 49 return response; 50 }) 51 .catch(() => { 52 return caches.match(request); 53 }) 54 ); 55 return; 56 } 57 58 // For static assets, use cache-first strategy 59 if (request.destination === 'image' || request.destination === 'script' || 60 request.destination === 'style' || request.destination === 'font') { 61 event.respondWith( 62 caches.match(request).then((cachedResponse) => { 63 if (cachedResponse) { 64 return cachedResponse; 65 } 66 return fetch(request).then((response) => { 67 // Cache successful responses 68 if (response.status === 200) { 69 const responseClone = response.clone(); 70 caches.open(RUNTIME_CACHE).then((cache) => { 71 cache.put(request, responseClone); 72 }); 73 } 74 return response; 75 }); 76 }) 77 ); 78 } 79});
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.