mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-06 05:55:21 +02:00
Update service-worker.js
Changes: - Optional chaining to avoid runtime errors - for...of for readability - Defensive checks for event.data and event.notification.data Signed-off-by: Blueray <83096078+blueraymusic@users.noreply.github.com>
This commit is contained in:
parent
321a343df4
commit
b62c6052c8
1 changed files with 45 additions and 0 deletions
|
@ -24,3 +24,48 @@
|
|||
// })
|
||||
// )
|
||||
// })
|
||||
|
||||
|
||||
// _____--------_____
|
||||
// Improve
|
||||
|
||||
self.addEventListener("push", async (event) => {
|
||||
if (!event.data) return
|
||||
|
||||
try {
|
||||
const { title, options } = await event.data.json()
|
||||
if (!title || !options) return
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(title, options)
|
||||
)
|
||||
} catch (error) {
|
||||
console.error(" Failed to Parse the Json:", error)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close()
|
||||
|
||||
const targetPath = event.notification?.data?.path
|
||||
if (!targetPath) return
|
||||
|
||||
event.waitUntil(
|
||||
clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
|
||||
for (const client of clientList) {
|
||||
const clientPath = new URL(client.url).pathname
|
||||
|
||||
if (clientPath === targetPath && "focus" in client) {
|
||||
return client.focus()
|
||||
}
|
||||
}
|
||||
|
||||
// No matching tab found, open new one
|
||||
if (clients.openWindow) {
|
||||
return clients.openWindow(targetPath)
|
||||
}
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue