const CACHE = 'kennedy-portal-v1'; const ARMAZON = [ '/pages/portal', ]; self.addEventListener('install', function (e) { e.waitUntil( caches.open(CACHE) .then(function (c) { return c.addAll(ARMAZON).catch(function () {}); }) .then(function () { return self.skipWaiting(); }) ); }); self.addEventListener('activate', function (e) { e.waitUntil( caches.keys().then(function (claves) { return Promise.all(claves.map(function (k) { if (k !== CACHE) return caches.delete(k); })); }).then(function () { return self.clients.claim(); }) ); }); self.addEventListener('fetch', function (e) { const req = e.request; if (req.method !== 'GET') return; let url; try { url = new URL(req.url); } catch (_) { return; } // Lo que NUNCA se guarda: Supabase, la API de Shopify, cualquier otro // servidor. Servir un stock o un precio de ayer como si fuera de ahora es // peor que no responder: se cotizaria con datos falsos sin saberlo. if (url.origin !== self.location.origin) return; if (url.pathname.indexOf('/rest/') === 0) return; if (url.pathname.indexOf('/api/') === 0) return; // Y nunca esta misma pagina: si se guardara una version vieja del service // worker, no habria forma de actualizarlo. if (url.pathname.indexOf('/pages/sw-kennedy') === 0) return; // Los archivos del portal: se responde con lo guardado y se refresca por // detras. Abre al instante y la proxima vez ya esta al dia. if (url.pathname.indexOf('/cdn/shop/') === 0 || url.pathname.indexOf('/pages/portal') === 0 || url.pathname.indexOf('/pages/vitrina') === 0 || url.pathname.indexOf('/pages/showroom') === 0) { e.respondWith( caches.open(CACHE).then(function (c) { return c.match(req).then(function (guardado) { const red = fetch(req).then(function (r) { if (r && r.status === 200) c.put(req, r.clone()); return r; }).catch(function () { return guardado; }); return guardado || red; }); }) ); return; } e.respondWith(fetch(req).catch(function () { return caches.match(req); })); });