1/* Service worker do Zappio (nuvem) â resiliência offline mÃnima. 2 * 3 * Objetivo: o DOMÃNIO abrir mesmo sem internet (casco do app em cache) para o 4 * componente de failover rodar e levar o aparelho ao nó local da loja. 5 * 6 * Estratégia conservadora: 7 * - Navegações (HTML): network-first com fallback ao index.html em cache â 8 * online sempre pega a versão nova (deploys não "grudam"); offline abre o casco. 9 * - Assets estáticos do próprio domÃnio (/static, hasheados pelo build): cache-first 10 * com preenchimento em runtime â imutáveis por nome, seguros de cachear. 11 * - API/terceiros: NUNCA intercepta (passa direto) â dados sempre frescos. 12 */ 13const CACHE = "zappio-shell-v1"; 14 15self.addEventListener("install", (event) => { 16 event.waitUntil(caches.open(CACHE).then((c) => c.addAll(["/", "/index.html"]).catch(() => {}))); 17 self.skipWaiting(); 18}); 19 20self.addEventListener("activate", (event) => { 21 event.waitUntil( 22 caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))) 23 .then(() => self.clients.claim()) 24 ); 25}); 26 27self.addEventListener("fetch", (event) => { 28 const req = event.request; 29 if (req.method !== "GET") return; 30 const url = new URL(req.url); 31 if (url.origin !== self.location.origin) return; // terceiros: não intercepta 32 if (url.pathname.startsWith("/api")) return; // API: sempre rede 33 34 // Navegação (abrir/atualizar página): rede primeiro;
34 sem rede, casco do cache. 35 if (req.mode === "navigate") { 36 event.respondWith( 37 fetch(req).then((res) => { 38 const copy = res.clone(); 39 caches.open(CACHE).then((c) => c.put("/index.html", copy)).catch(() => {}); 40 return res; 41 }).catch(() => caches.match("/index.html").then((hit) => hit || Response.error())) 42 ); 43 return; 44 } 45 46 // Assets do build (nomes hasheados) + Ãcones/manifest: cache-first com runtime fill. 47 if (url.pathname.startsWith("/static/") || /\.(js|css|png|svg|ico|woff2?)$/.test(url.pathname)) { 48 event.respondWith( 49 caches.match(req).then((hit) => hit || fetch(req).then((res) => { 50 if (res.ok) { 51 const copy = res.clone(); 52 caches.open(CACHE).then((c) => c.put(req, copy)).catch(() => {}); 53 } 54 return res; 55 })) 56 ); 57 } 58});
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.