JoseAndresLopez's picture
Subir aplicacion
4ab46a7 verified
Raw History Blame Contribute Delete
1.17 kB
/* Service worker — cachea el "app shell" para que la interfaz cargue
también sin conexión. Los datos de /api se piden a la red y, si falla,
se degradan con elegancia en el cliente. */
const CACHE = "monitor-telescopio-v1";
const SHELL = [
"/",
"/static/styles.css",
"/static/app.js",
"/manifest.json",
"/static/icons/icon-192.png",
"/static/icons/icon-512.png"
];
self.addEventListener("install", (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (e) => {
const url = new URL(e.request.url);
// La API siempre va a la red (datos frescos); el shell, cache-first.
if (url.pathname.startsWith("/api/")) {
e.respondWith(fetch(e.request).catch(() => new Response("{}", {
headers: { "Content-Type": "application/json" }
})));
return;
}
e.respondWith(caches.match(e.request).then((r) => r || fetch(e.request)));
});