1const CACHE_NAME = "masdar-pos-shell-v1"; 2const APP_SHELL = ["/", "/masdar-pos/login", "/masdar-pos/sale"]; 3 4self.addEventListener("install", event => { 5 event.waitUntil( 6 caches.open(CACHE_NAME) 7 .then(cache => cache.addAll(APP_SHELL)) 8 .then(() => self.skipWaiting()) 9 ); 10}); 11 12self.addEventListener("activate", event => { 13 event.waitUntil( 14 caches.keys() 15 .then(keys => Promise.all(keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key)))) 16 .then(() => self.clients.claim()) 17 ); 18}); 19 20self.addEventListener("fetch", event => { 21 const request = event.request; 22 if (request.method !== "GET" || new URL(request.url).origin !== self.location.origin) return; 23 const url = new URL(request.url); 24 if (url.pathname.startsWith("/api/")) return; 25 26 if (request.mode === "navigate") { 27 event.respondWith( 28 fetch(request) 29 .then(response => { 30 const copy = response.clone(); 31 void caches.open(CACHE_NAME).then(cache => cache.put(request, copy)); 32 return response; 33 }) 34 .catch(() => caches.match(request).then(cached => cached || caches.match("/"))) 35 ); 36 return; 37 } 38 39 event.respondWith( 40 caches.match(request).then(cached => { 41 const network = fetch(request) 42 .then(response => { 43 if (response.ok) { 44 const copy = response.clone(); 45 void caches.open(CACHE_NAME).then(cache => cache.put(request, copy)); 46 } 47 return response; 48 }) 49 .catch(() => cached); 50 return cached || network; 51 }) 52 ); 53});
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.