1// Service Worker - Diario Ahora 2// 1. Push notifications 3// 4// El caché offline de PDFs de edición se RETIRà a propósito: el endpoint 5// /api/edition/pdf ahora tiene MURO (devuelve solo portada+deportes a lectores 6// anónimos y la edición completa a registrados, según la cookie de sesión). 7// Un caché cache-first keyeado por URL servÃa la variante de la sesión 8// equivocada â incluso el PDF completo a un anónimo en un navegador donde antes 9// se logueó alguien. Por eso las requests del PDF pasan derecho a la red, que 10// es la única que aplica el muro. (Si en el futuro se quiere lectura offline 11// para registrados, hay que keyear el caché por un flag de sesión visible al 12// cliente, no por URL.) 13// 14// Este archivo debe estar en /public para que el scope sea '/'. 15 16// Caché viejo (v1) que pudo guardar la edición completa: se purga en activate. 17const LEGACY_PDF_CACHE = 'edicion-pdfs-v1'; 18 19self.addEventListener('install', (event) => { 20 event.waitUntil(self.skipWaiting()); 21}); 22 23self.addEventListener('activate', (event) => { 24 event.waitUntil( 25 (async () => { 26 // Borrar el caché de PDFs anterior (podÃa contener la edición completa 27 // accesible sin sesión). Seguridad: limpiar al activar el SW nuevo. 28 await caches.delete(LEGACY_PDF_CACHE).catch(() => { /* no existe â ok */ }); 29 await self.clients.claim(); 30 })(), 31 ); 32}); 33 34self.addEventListener('push', (event) => { 35 let data = {}; 36 try { 37 data = event.data ? event.data.json() : {}; 38 } catch { 39 data = { title: 'Diario Ahora', body: event.data ? event.data.text() : '' }; 40 } 41 42 const title = data.title || 'Diario Ahora'; 43 const options = { 44 body: data.body || '', 45 icon: '/img/logodiario.png', 46 badge: '/img/logodiario.png', 47 data: { url: data.url || '/' }, 48 tag: data.tag || 'news', 49 renotify: true, 50 vibrate: [200, 100, 200], 51 }; 52 53 event.waitUntil(self.registration.showNotification(title, options)); 54}); 55 56self.addEventListener('notificationclick', (event) => { 57 event.notification.close(); 58 const url = event.notification.data?.url || '/'; 59 60 event.waitUntil( 61 clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { 62 // Si ya hay una pestaña abierta, navegar a la URL 63 for (const client of clientList) { 64 if (client.url.includes(self.location.origin) && 'focus' in client) { 65 client.navigate(url); 66 return client.focus(); 67 } 68 } 69 // Si no, abrir nueva pestaña 70 return clients.openWindow(url); 71 }) 72 ); 73});
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.