Files
hereconnect/apps/frontend/public/service-worker.js
T

135 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-12-05 21:06:57 +02:00
const VER = 14
2024-12-05 11:04:04 +02:00
self.addEventListener('message', async (event) => {
2024-12-05 21:06:57 +02:00
event.waitUntil(debug(Object.assign({ answer: true }, event.data)))
2024-12-05 11:04:04 +02:00
})
2024-11-21 16:57:05 +02:00
self.addEventListener('install', (event) => {
// The promise that skipWaiting() returns can be safely ignored.
2024-12-05 21:06:57 +02:00
event.waitUntil(self.skipWaiting().then(() => debug({ installed: true })))
2024-11-21 16:57:05 +02:00
// Perform any other actions required for your
// service worker to install, potentially inside
// of event.waitUntil();
})
2024-11-25 22:58:10 +02:00
self.addEventListener('activate', (event) => {
event.waitUntil(
2024-12-05 11:04:04 +02:00
// Привязка нового сервис-воркера к текущим клиентам
clients.claim().then(() => debug(event.source, { activated: true })),
2024-11-25 22:58:10 +02:00
)
})
2024-12-05 21:06:57 +02:00
self.addEventListener('push', async (event) => {
debug({
event: 'push',
})
2024-12-05 11:04:04 +02:00
// 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) {
2024-12-05 21:06:57 +02:00
debug({
error: 'Received WebPush with an empty title. Received body',
})
2024-11-21 01:03:01 +02:00
}
2024-12-05 21:06:57 +02:00
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,
})
})
2024-12-05 14:11:11 +02:00
})
self.addEventListener('notificationclick', (event) => {
2024-12-05 21:06:57 +02:00
debug({
event: 'notificationclick',
})
2024-12-05 14:11:11 +02:00
event.waitUntil(handleNotificationClick(event))
2024-11-21 01:03:01 +02:00
})
2024-12-05 11:04:04 +02:00
async function handleNotificationClick(event) {
2024-12-01 22:30:40 +02:00
event.notification.close()
2024-12-05 11:04:04 +02:00
if (!event.notification.data) {
2024-12-05 21:06:57 +02:00
await debug({
2024-12-05 11:04:04 +02:00
error: 'Click on WebPush with empty data, where url should be. Notification',
})
return
}
2024-12-01 22:30:40 +02:00
if (!event.notification.data.url) {
2024-12-05 21:06:57 +02:00
await debug({ error: 'Click on WebPush without url. Notification' })
2024-12-01 22:30:40 +02:00
return
}
2024-12-05 11:04:04 +02:00
await openURL(event.notification.data.url)
}
2024-11-21 01:03:01 +02:00
2024-12-05 11:04:04 +02:00
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 {
2024-12-05 21:06:57 +02:00
await debug(client, {
urlToOpen,
'client.url': client.url,
})
2024-12-02 23:19:20 +02:00
if (
2024-12-05 11:04:04 +02:00
urlToOpenObj.pathname + urlToOpenObj.search !==
2024-12-02 23:19:20 +02:00
clientUrlObj.pathname + clientUrlObj.search
) {
2024-12-05 11:04:04 +02:00
continue
2024-11-21 16:11:23 +02:00
}
2024-12-03 00:57:27 +02:00
2024-12-05 11:04:04 +02:00
if (client.focused) {
2024-12-05 21:06:57 +02:00
debug({ 'client.focused': true })
2024-12-05 11:04:04 +02:00
return
2024-12-02 19:08:39 +02:00
}
2024-12-05 11:04:04 +02:00
await client.focus()
2024-12-05 21:06:57 +02:00
debug({ 'client.focus()': true })
2024-12-05 11:04:04 +02:00
return
} catch (error) {
await debug({ error: error.message })
2024-12-02 19:08:39 +02:00
}
2024-12-05 11:04:04 +02:00
}
} catch (error) {
2024-12-05 21:06:57 +02:00
debug({ 'get client error': error })
2024-12-05 11:04:04 +02:00
}
} catch (error) {
2024-12-05 21:06:57 +02:00
debug({ 'openURL error': error })
}
if (matchedClients && matchedClients.length) {
debug({ 'no matched clients': true })
2024-12-05 11:04:04 +02:00
}
2024-12-02 18:57:07 +02:00
2024-12-05 11:04:04 +02:00
try {
const client = await clients.openWindow(urlToOpen)
if (!client) {
2024-12-05 21:06:57 +02:00
await debug({ error: 'client is null' })
2024-12-05 11:04:04 +02:00
}
2024-12-05 21:06:57 +02:00
debug({ 'clients.openWindow()': true })
2024-12-05 11:04:04 +02:00
} catch (error) {
await debug(matchedClients && matchedClients[0], { error: error.message })
}
}
2024-12-05 21:06:57 +02:00
function debug() {}
// async function debug(...args) {
// return fetch('https://hereconnect-debug.condev.ru/debug', {
// method: 'POST',
// body: JSON.stringify(args),
// })
// }