const VER = 14 self.addEventListener('message', async (event) => { event.waitUntil(debug(Object.assign({ answer: true }, event.data))) }) self.addEventListener('install', (event) => { // The promise that skipWaiting() returns can be safely ignored. event.waitUntil(self.skipWaiting().then(() => debug({ installed: true }))) // Perform any other actions required for your // service worker to install, potentially inside // of event.waitUntil(); }) self.addEventListener('activate', (event) => { event.waitUntil( // Привязка нового сервис-воркера к текущим клиентам clients.claim().then(() => debug(event.source, { activated: true })), ) }) self.addEventListener('push', async (event) => { debug({ event: 'push', }) // PushData keys structure standart https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification let pushData = event.data.json() if (!pushData || !pushData.notification.title) { debug({ error: 'Received WebPush with an empty title. Received body', }) } self.registration .showNotification(pushData.notification.title, pushData.notification.options) .then(function () { // You can save to your analytics fact that push was shown // fetch('https://your_backend_server.com/track_show?message_id=' + pushData.data.message_id); }) .catch((error) => { debug({ errorShowNotification: error.message, }) }) }) self.addEventListener('notificationclick', (event) => { debug({ event: 'notificationclick', }) event.waitUntil(handleNotificationClick(event)) }) async function handleNotificationClick(event) { event.notification.close() if (!event.notification.data) { await debug({ error: 'Click on WebPush with empty data, where url should be. Notification', }) return } if (!event.notification.data.url) { await debug({ error: 'Click on WebPush without url. Notification' }) return } await openURL(event.notification.data.url) } async function openURL(urlToOpen) { let matchedClients try { const urlToOpenObj = new URL(urlToOpen, location.origin) matchedClients = await clients.matchAll({ type: 'window', includeUncontrolled: true }) try { for (const client of matchedClients) { const clientUrlObj = new URL(client.url) try { await debug(client, { urlToOpen, 'client.url': client.url, }) if ( urlToOpenObj.pathname + urlToOpenObj.search !== clientUrlObj.pathname + clientUrlObj.search ) { continue } if (client.focused) { debug({ 'client.focused': true }) return } await client.focus() debug({ 'client.focus()': true }) return } catch (error) { await debug({ error: error.message }) } } } catch (error) { debug({ 'get client error': error }) } } catch (error) { debug({ 'openURL error': error }) } if (matchedClients && matchedClients.length) { debug({ 'no matched clients': true }) } try { const client = await clients.openWindow(urlToOpen) if (!client) { await debug({ error: 'client is null' }) } debug({ 'clients.openWindow()': true }) } catch (error) { await debug(matchedClients && matchedClients[0], { error: error.message }) } } function debug() {} // async function debug(...args) { // return fetch('https://hereconnect-debug.condev.ru/debug', { // method: 'POST', // body: JSON.stringify(args), // }) // }