1// Service Worker for Push Notifications 2// Editor80 Public Site 3 4self.addEventListener('install', function(event) { 5 self.skipWaiting(); 6}); 7 8self.addEventListener('activate', function(event) { 9 event.waitUntil(self.clients.claim()); 10}); 11 12function track(notificationId, type) { 13 if (!notificationId) return Promise.resolve(); 14 return fetch('/api/push/track', { 15 method: 'POST', 16 headers: { 'Content-Type': 'application/json' }, 17 body: JSON.stringify({ notificationId, type }), 18 keepalive: true, 19 }).catch(() => {}); 20} 21 22self.addEventListener('push', function(event) { 23 if (!event.data) return; 24 25 let data; 26 try { 27 data = event.data.json(); 28 } catch (e) { 29 data = { title: 'Nueva noticia', body: event.data.text() }; 30 } 31 32 const notificationId = data.notificationId || null; 33 34 const options = { 35 body: data.body || '', 36 icon: data.icon || '/favicon.ico', 37 badge: '/favicon.ico', 38 data: { url: data.url || '/', notificationId }, 39 vibrate: [200, 100, 200], 40 tag: data.tag || 'news-' + Date.now(), 41 renotify: true, 42 }; 43 44 event.waitUntil( 45 Promise.all([ 46 self.registration.showNotification(data.title || 'Noticia', options), 47 track(notificationId, 'delivered'), 48 ]) 49 ); 50}); 51 52self.addEventListener('notificationclick', function(event) { 53 event.notification.close(); 54 55 const url = event.notification.data?.url || '/'; 56 const notificationId = event.notification.data?.notificationId || null; 57 58 event.waitUntil( 59 Promise.all([ 60 track(notificationId, 'clicked'), 61 clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clientList) { 62 for (const client of clientList) { 63 if (client.url === url && 'focus' in client) { 64 return client.focus(); 65 } 66 } 67 if (clients.openWindow) { 68 return clients.openWindow(url); 69 } 70 }), 71 ]) 72 ); 73}); 74 75// Note: no pushsubscriptionchange handler â the SW does not have access to the 76// VAPID applicationServerKey, so an auto re-subscribe would fail with 77// "missing applicationServerKey". The banner re-prompts on next visit.
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.