Files
hereconnect/apps/frontend/public/service-worker.js
T
ti de8496395b
Deploy frontend / frontend (push) Successful in 1m8s
open url by send message to page
2024-12-02 18:57:07 +02:00

151 lines
4.7 KiB
JavaScript

self.addEventListener('install', (event) => {
console.log('install event:', event)
// The promise that skipWaiting() returns can be safely ignored.
self.skipWaiting()
// Perform any other actions required for your
// service worker to install, potentially inside
// of event.waitUntil();
})
self.addEventListener('activate', (event) => {
event.waitUntil(
clients.claim(), // Привязка нового сервис-воркера к текущим клиентам
)
})
self.addEventListener('push', (event) => {
const data = event.data.json()
console.log('Push событие получено:', data.type)
// Отображение уведомления
if (data.type === 'notification') {
event.waitUntil(
self.registration
.showNotification(data.notification.title, data.notification.options)
.then(() => {
console.log('Notification создан', data.notification.options.tag)
})
.catch((error) => {
console.error('Notification ошибка создания', error)
}),
)
} else if (data.type === 'cancelNotification') {
event.waitUntil(
self.registration
.getNotifications({ tag: data.notification.options.tag })
.then((notifications) => {
notifications.forEach((notification) => {
if (
notification.tag === data.notification.options.tag ||
notification.data.payload.tag === data.notification.options.data.payload.tag // удалить, если notification.tag поддерживают все
) {
notification.close()
console.log('Notification удален')
}
})
}),
)
}
})
self.addEventListener('notificationclose', function (event) {
const dismissedNotification = event.notification
console.log('dismissedNotification', dismissedNotification)
})
self.addEventListener('notificationclick', (event) => {
console.log('click to notification', event.notification)
event.notification.close()
// const urlToOpen = new URL(event.notification.data.url, self.location.origin).href
if (!event.notification.data.url) {
console.log('no urlToOpen')
return
}
function getNotificationUrl(urlToOpen) {
return urlToOpen
// return fetch(urlToOpen, {
// redirect: 'manual',
// method: 'HEAD',
// cache: 'no-cache',
// })
// .then((response) => {
// console.log('response.url', response.url)
// console.log('response.type', response.url)
// if (response && response.type === 'opaqueredirect') {
// const location = response.headers.get('location')
// console.log('headers location', location)
// return location
// }
// return response.url
// })
// .catch((error) => {
// console.debug('erro in getNotificationUrl', error)
// return urlToOpen
// })
}
// Открытие URL из данных уведомления
event.waitUntil(
Promise.all([
getNotificationUrl(event.notification.data.url),
clients.matchAll({ type: 'window', includeUncontrolled: true }),
])
.then(([urlToOpen, clientList]) => {
for (const client of clientList) {
console.log('client.url', client.url)
if (client.url === urlToOpen && 'focus' in client) {
console.log('focus')
return Promise.all([
client.postMessage({
type: 'notificationClick',
tag: event.notification.tag,
payload: event.notification.payload,
matchUrl: true,
}),
client.focus(),
])
}
}
return Promise.resolve()
.then(() => {
if (!clients.openWindow) {
throw new Error('clients.openWindow not available')
}
return clients.openWindow(urlToOpen).then((newClient) => {
if (!newClient) {
throw new Error('new window not opened')
}
})
})
.catch((error) => {
console.debug('Error', error)
for (const client of clientList) {
return Promise.all([
client.postMessage({
type: 'notificationClick',
url: urlToOpen,
tag: event.notification.tag,
payload: event.notification.payload,
matchUrl: false,
msg: 'clients.openWindow not available',
}),
client.focus(),
])
}
})
})
.catch((error) => {
console.error(error)
}),
)
})