fix(05-review): CR-03 notificationclick uses matchAll+focus+navigate for deep-link URLs

This commit is contained in:
Lucas Berger
2026-06-09 22:22:27 -04:00
parent 7702f7e19a
commit e5f7b1ab7c
+17 -6
View File
@@ -134,8 +134,15 @@ self.addEventListener('push', (event: PushEvent) => {
// notificationclick handler (D-14 — deep-link on tap).
//
// Closes the notification, then:
// 1. If a window is already open at the target URL, focus it.
// 2. Otherwise, open a new window at the target URL.
// 1. Find any existing same-origin window with clients.matchAll (not exact-URL
// match — deep-link URLs differ from the current window URL). Focus it and
// navigate it to the target URL so the deep-link is honoured (CR-03 fix).
// 2. If no window exists, open a new one at the target URL.
//
// CR-03: The previous exact-URL match (client.url === url) never fired for
// deep-link notifications whose query string differed from the current window URL
// (e.g. notification for /calendar?event=uid while window is at /calendar).
// Using any existing window + navigate() satisfies D-14 on both Android and iOS.
// ---------------------------------------------------------------------------
self.addEventListener('notificationclick', (event: NotificationEvent) => {
event.notification.close()
@@ -149,13 +156,17 @@ self.addEventListener('notificationclick', (event: NotificationEvent) => {
self.clients
.matchAll({ type: 'window', includeUncontrolled: true })
.then((clientList) => {
// Focus an existing window already at the target URL
// Focus any existing window on this origin and navigate it to the target URL.
// We don't match on client.url — the deep-link URL will almost always differ
// from the current window location (query string with event uid / date).
for (const client of clientList) {
if (client.url === url && 'focus' in client) {
return (client as WindowClient).focus()
if ('focus' in client) {
return (client as WindowClient).focus().then(() =>
(client as WindowClient).navigate(url)
)
}
}
// No existing window — open a new one
// No existing window — open a new one at the deep-link URL
if (self.clients.openWindow) {
return self.clients.openWindow(url)
}