Fix: service worker notification URL handling to gracefully manage missing URLs.
This commit is contained in:
parent
8ff24135d1
commit
ab24bd3454
@ -23,7 +23,7 @@ self.addEventListener('push', (event) => {
|
|||||||
vibrate: [200, 100, 200],
|
vibrate: [200, 100, 200],
|
||||||
requireInteraction: true,
|
requireInteraction: true,
|
||||||
data: {
|
data: {
|
||||||
url: notification.url
|
url: notification.url || null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -32,17 +32,19 @@ self.addEventListener('push', (event) => {
|
|||||||
self.addEventListener('notificationclick', (event) => {
|
self.addEventListener('notificationclick', (event) => {
|
||||||
event.notification.close();
|
event.notification.close();
|
||||||
|
|
||||||
|
const url = event.notification.data && event.notification.data.url;
|
||||||
|
|
||||||
|
if (!url) return;
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
||||||
// Check if there's already a tab open with this URL
|
|
||||||
for (const client of clientList) {
|
for (const client of clientList) {
|
||||||
if (client.url === event.notification.data.url && 'focus' in client) {
|
if (client.url === url && 'focus' in client) {
|
||||||
return client.focus();
|
return client.focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If not, open a new window
|
|
||||||
if (clients.openWindow) {
|
if (clients.openWindow) {
|
||||||
return clients.openWindow(event.notification.data.url);
|
return clients.openWindow(url);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user