update service worker
Main daploy / deploy (push) Successful in 53s

This commit is contained in:
2024-11-21 16:51:51 +02:00
parent 75be87c144
commit 458e5e34d8
+21 -11
View File
@@ -5,14 +5,14 @@ self.addEventListener('push', (event) => {
// Отображение уведомления
if (data.type === 'notification') {
event.waitUntil(
self.registration.showNotification(data.notification.title, data.notification.options).then(
() => {
self.registration
.showNotification(data.notification.title, data.notification.options)
.then(() => {
console.log('Notification создан', data.notification.options.tag)
},
(e) => {
console.error('Notification ошибка создания', e)
},
),
})
.catch((error) => {
console.error('Notification ошибка создания', error)
}),
)
} else if (data.type === 'cancelNotification') {
event.waitUntil(
@@ -33,9 +33,16 @@ self.addEventListener('push', (event) => {
}
})
self.addEventListener('notificationclose', function (event) {
const dismissedNotification = event.notification
console.log('dismissedNotification', dismissedNotification)
})
self.addEventListener('notificationclick', (event) => {
console.log('клик на уведомление', event.notification)
event.notification.close()
const urlToOpen = new URL(event.notification.data.url, self.location.origin).href
// Открытие URL из данных уведомления
event.waitUntil(
@@ -44,7 +51,7 @@ self.addEventListener('notificationclick', (event) => {
.then((clientList) => {
for (const client of clientList) {
console.log('client.url', client.url)
if (client.url === event.notification.data.url && 'focus' in client) {
if (client.url === urlToOpen && 'focus' in client) {
console.log('фокус')
return Promise.all([
@@ -58,8 +65,8 @@ self.addEventListener('notificationclick', (event) => {
}
}
if (clients.openWindow) {
console.log('новое окно')
return clients.openWindow(event.notification.data.url).then((client) => {
console.log('новое окно', urlToOpen)
return clients.openWindow(urlToOpen).then((client) => {
if (client) {
return client.focus()
}
@@ -67,6 +74,9 @@ self.addEventListener('notificationclick', (event) => {
})
}
})
.then(() => {
return event.notification.close()
})
.catch((error) => {
console.error(error)
}),