| 1 | // Service worker: que la app ABRA sin red una vez visitada (el mismo fichero en el Translator y |
| 2 | // en el Copiloto; cada uno lo sirve desde su raíz). |
| 3 | // |
| 4 | // Guarda la página y sus módulos (red primero: con red siempre se sirve lo último desplegado) y |
| 5 | // el motor de jsDelivr, que va con versión fija y no cambia (caché primero). Los modelos NO pasan |
| 6 | // por aquí: los guarda el almacén compartido de models.elffuss.com, que tiene su propio service |
| 7 | // worker. Todo lo demás del sitio (el blog, por ejemplo) no se toca. |
| 8 | // |
| 9 | // Cambiar VERSION si cambia la lista o la versión fijada del motor: el despliegue la renueva. |
| 10 | const VERSION = '1'; |
| 11 | const PREFIJO = 'elffuss-app-' + new URL(self.registration.scope).hostname + '-'; |
| 12 | const CACHE = PREFIJO + VERSION; |
| 13 | const PROPIOS = ['./', 'diarize.mjs', 'splash-gl.js', 'model-cache.mjs', 'model-broker.js', 'manifest.webmanifest', 'icon.svg', |
| 14 | 'copilot-llm.mjs', 'copilot-grammar.mjs']; |
| 15 | const MOTOR = 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1'; |
| 16 | |
| 17 | const abs = p => new URL(p, self.registration.scope).href; |
| 18 | const RAIZ = abs('./'); |
| 19 | const RUTAS = new Set(PROPIOS.map(p => new URL(abs(p)).pathname).concat(new URL(abs('index.html')).pathname)); |
| 20 | |
| 21 | self.addEventListener('install', e => { |
| 22 | // Lo que falte (el Translator no tiene los dos módulos del Copiloto) no impide instalar. |
| 23 | e.waitUntil(caches.open(CACHE) |
| 24 | .then(c => Promise.all(PROPIOS.map(p => c.add(abs(p)).catch(() => {})))) |
| 25 | .then(() => self.skipWaiting())); |
| 26 | }); |
| 27 | self.addEventListener('activate', e => { |
| 28 | e.waitUntil(caches.keys() |
| 29 | .then(ks => Promise.all(ks.filter(k => k.startsWith(PREFIJO) && k !== CACHE).map(k => caches.delete(k)))) |
| 30 | .then(() => self.clients.claim())); |
| 31 | }); |
| 32 | |
| 33 | async function redPrimero(req, clave) { |
| 34 | try { |
| 35 | const res = await fetch(req); |
| 36 | if (res.ok) { const copia = res.clone(); caches.open(CACHE).then(c => c.put(clave, copia)).catch(() => {}); } |
| 37 | return res; |
| 38 | } catch (err) { |
| 39 | const guardada = await caches.match(clave, { ignoreSearch: true }); |
| 40 | if (guardada) return guardada; |
| 41 | throw err; |
| 42 | } |
| 43 | } |
| 44 | async function cachePrimero(req) { |
| 45 | const guardada = await caches.match(req.url); |
| 46 | if (guardada) return guardada; |
| 47 | const res = await fetch(req); |
| 48 | if (res.ok) { const copia = res.clone(); caches.open(CACHE).then(c => c.put(req.url, copia)).catch(() => {}); } |
| 49 | return res; |
| 50 | } |
| 51 | |
| 52 | self.addEventListener('fetch', e => { |
| 53 | const r = e.request; |
| 54 | if (r.method !== 'GET') return; |
| 55 | const u = new URL(r.url); |
| 56 | if (r.mode === 'navigate') { |
| 57 | if (u.origin === location.origin && (u.pathname === new URL(RAIZ).pathname || u.pathname === new URL(abs('index.html')).pathname)) e.respondWith(redPrimero(r, RAIZ)); |
| 58 | return; |
| 59 | } |
| 60 | if (u.origin === location.origin && RUTAS.has(u.pathname)) e.respondWith(redPrimero(r, u.origin + u.pathname)); |
| 61 | else if (r.url.startsWith(MOTOR + '/') || r.url === MOTOR) e.respondWith(cachePrimero(r)); |
| 62 | }); |
| 63 | |
| 64 | // La página pide guardar el motor cuando ya lo ha usado (por si este service worker aún no |
| 65 | // estaba activo cuando lo importó). |
| 66 | self.addEventListener('message', e => { |
| 67 | const urls = (e.data && e.data.tipo === 'guardar' && Array.isArray(e.data.urls)) ? e.data.urls.filter(u => typeof u === 'string' && (u === MOTOR || u.startsWith(MOTOR + '/'))) : []; |
| 68 | if (!urls.length) return; |
| 69 | e.waitUntil(caches.open(CACHE).then(c => Promise.all(urls.map(async u => { |
| 70 | if (await c.match(u)) return; |
| 71 | const res = await fetch(u, { mode: 'cors' }).catch(() => null); |
| 72 | if (res && res.ok) await c.put(u, res); |
| 73 | })))); |
| 74 | }); |