support redirect in notifications
Main daploy / deploy (push) Failing after 8s

This commit is contained in:
2024-12-01 22:30:40 +02:00
parent c20429a8ff
commit 2ae13f6ec4
+51 -19
View File
@@ -56,42 +56,74 @@ self.addEventListener('notificationclose', function (event) {
}) })
self.addEventListener('notificationclick', (event) => { self.addEventListener('notificationclick', (event) => {
console.log('клик на уведомление', event.notification) console.log('click to notification', event.notification)
const urlToOpen = new URL(event.notification.data.url, self.location.origin).href 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 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 из данных уведомления // Открытие URL из данных уведомления
event.waitUntil( event.waitUntil(
clients Promise.all([
.matchAll({ type: 'window', includeUncontrolled: true }) getNotificationUrl(event.notification.data.url),
.then((clientList) => {
clients.matchAll({ type: 'window', includeUncontrolled: true }),
])
.then(([urlToOpen, clientList]) => {
for (const client of clientList) { for (const client of clientList) {
console.log('client.url', client.url) console.log('client.url', client.url)
if (client.url === urlToOpen && 'focus' in client) { if (client.url === urlToOpen && 'focus' in client) {
console.log('фокус') console.log('focus')
return Promise.all([ return client.focus()
client.focus(), // return Promise.all([
client.postMessage({ // client.focus(),
type: 'notificationClick', // client.postMessage({
tag: event.notification.tag, // type: 'notificationClick',
payload: event.notification.payload, // tag: event.notification.tag,
}), // payload: event.notification.payload,
]) // }),
// ])
} }
} }
if (clients.openWindow) { if (clients.openWindow) {
console.log('новое окно', urlToOpen) console.log('open new window', urlToOpen)
return clients.openWindow(urlToOpen).then((client) => { return clients.openWindow(urlToOpen).then((client) => {
if (!client) { if (!client) {
console.log('новое окно не открылось') console.log('new window not opened')
} }
}) })
} else {
console.log('clients.openWindow not available')
} }
}) })
.then(() => {
return event.notification.close()
})
.catch((error) => { .catch((error) => {
console.error(error) console.error(error)
}), }),