open url by send message to page
Deploy frontend / frontend (push) Successful in 1m8s

This commit is contained in:
2024-12-02 18:57:07 +02:00
parent 4f0a013014
commit de8496395b
2 changed files with 66 additions and 37 deletions
+55 -36
View File
@@ -68,25 +68,26 @@ self.addEventListener('notificationclick', (event) => {
} }
function getNotificationUrl(urlToOpen) { function getNotificationUrl(urlToOpen) {
return fetch(urlToOpen, { return urlToOpen
redirect: 'manual', // return fetch(urlToOpen, {
method: 'HEAD', // redirect: 'manual',
cache: 'no-cache', // method: 'HEAD',
}) // cache: 'no-cache',
.then((response) => { // })
console.log('response.url', response.url) // .then((response) => {
console.log('response.type', response.url) // console.log('response.url', response.url)
if (response && response.type === 'opaqueredirect') { // console.log('response.type', response.url)
const location = response.headers.get('location') // if (response && response.type === 'opaqueredirect') {
console.log('headers location', location) // const location = response.headers.get('location')
return location // console.log('headers location', location)
} // return location
return response.url // }
}) // return response.url
.catch((error) => { // })
console.debug('erro in getNotificationUrl', error) // .catch((error) => {
return urlToOpen // console.debug('erro in getNotificationUrl', error)
}) // return urlToOpen
// })
} }
// Открытие URL из данных уведомления // Открытие URL из данных уведомления
@@ -102,28 +103,46 @@ self.addEventListener('notificationclick', (event) => {
if (client.url === urlToOpen && 'focus' in client) { if (client.url === urlToOpen && 'focus' in client) {
console.log('focus') console.log('focus')
return client.focus() return Promise.all([
// 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, matchUrl: true,
// payload: event.notification.payload, }),
// }), client.focus(),
// ]) ])
} }
} }
if (clients.openWindow) { return Promise.resolve()
console.log('open new window', urlToOpen) .then(() => {
return clients.openWindow(urlToOpen).then((client) => { if (!clients.openWindow) {
if (!client) { throw new Error('clients.openWindow not available')
console.log('new window not opened') }
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(),
])
} }
}) })
} else {
console.log('clients.openWindow not available')
}
}) })
.catch((error) => { .catch((error) => {
console.error(error) console.error(error)
}), }),
+11 -1
View File
@@ -65,7 +65,17 @@ app.use(PrimeVue, {
app.mount('#app') app.mount('#app')
registerServiceWorker() registerServiceWorker()
.then(() => console.log('Service Worker успешно зарегистрирован')) .then((registration) => {
console.log('Service Worker успешно зарегистрирован')
navigator.serviceWorker.addEventListener('message', (event) => {
console.debug('serviceWorker message', event.data)
if (event.data.type === 'notificationClick' && !event.data.matchUrl) {
const url = new URL(event.data.url, location.origin)
console.debug('go to', `${url.pathname}${url.search}${url.hash}`)
router.push(`${url.pathname}${url.search}${url.hash}`)
}
})
})
.catch(console.error) .catch(console.error)
window.PouchDB = PouchDB // it for debug in console only window.PouchDB = PouchDB // it for debug in console only