1/** 2 * AliTariffs service worker â handles browser push notifications. 3 * Served from / so it controls the entire site origin. 4 */ 5 6self.addEventListener('install', (event) => { 7 self.skipWaiting(); 8}); 9 10self.addEventListener('activate', (event) => { 11 event.waitUntil(self.clients.claim()); 12}); 13 14self.addEventListener('push', (event) => { 15 let data = {}; 16 try { 17 data = event.data ? event.data.json() : {}; 18 } catch { 19 data = { title: 'AliTariffs', body: event.data ? event.data.text() : '' }; 20 } 21 22 const title = data.title || 'AliTariffs â Weekly deal'; 23 const options = { 24 body: data.body || 'New AliExpress deal â tap to see the landed cost.', 25 icon: data.icon || '/favicon.svg', 26 badge: data.badge || '/favicon.svg', 27 image: data.image, 28 tag: data.tag || 'alitariffs-weekly', 29 renotify: true, 30 requireInteraction: false, 31 data: { 32 url: data.url || 'https://alitariffs.com/', 33 productUrl: data.productUrl 34 }, 35 actions: data.url 36 ? [{ action: 'open', title: 'View deal' }, { action: 'close', title: 'Dismiss' }] 37 : undefined 38 }; 39 40 event.waitUntil(self.registration.showNotification(title, options)); 41}); 42 43self.addEventListener('notificationclick', (event) => { 44 event.notification.close(); 45 if (event.action === 'close') return; 46 const url = event.notification.data?.url || 'https://alitariffs.com/'; 47 event.waitUntil( 48 self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { 49 // Focus an existing tab if one is open 50 for (const client of clientList) { 51 if (client.url === url && 'focus' in client) return client.focus(); 52 } 53 // Otherwise open a new one 54 if (self.clients.openWindow) return self.clients.openWindow(url); 55 }) 56 ); 57}); 58 59self.addEventListener('pushsubscriptionchange', (event) => { 60 // Re-subscribe with the new endpoint when the browser rotates it 61 event.waitUntil( 62 self.registration.pushManager 63 .subscribe({ 64 userVisibleOnly: true, 65 applicationServerKey: event.oldSubscription?.options?.applicationServerKey 66 }) 67 .then((newSub) => 68 fetch('/api/push/subscribe', { 69 method: 'POST', 70 headers: { 'Content-Type': 'application/json' }, 71 body: JSON.stringify({ subscription: newSub.toJSON() }) 72 }) 73 ) 74 ); 75});
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.