From 1a5ca81513a774bf08aecfef8abbb049e0c1b7f0 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Sat, 12 Sep 2026 20:28:42 -0400 Subject: [PATCH] fix(desktop): notification click prefers the focused/visible window Fixes #78 Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA --- src/sw.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sw.ts b/src/sw.ts index 2b713dda2..947d38613 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -149,11 +149,19 @@ self.addEventListener('notificationclick', (event: NotificationEvent) => { event.waitUntil( (async () => { - const windowClients = await self.clients.matchAll({ - type: 'window', - includeUncontrolled: true, - }); - const client = windowClients.find((c): c is WindowClient => 'focus' in c); + const windowClients = ( + await self.clients.matchAll({ + type: 'window', + includeUncontrolled: true, + }) + ).filter((c): c is WindowClient => 'focus' in c); + // #78 — prefer a client that's actually focused, then one that's merely + // visible, before falling back to whatever matchAll() returned first (its + // ordering is unspecified across browsers and not "most recently used"). + const client = + windowClients.find((c) => c.focused) ?? + windowClients.find((c) => c.visibilityState === 'visible') ?? + windowClients[0]; if (client) { await client.focus(); if (path) client.postMessage({ type: 'notificationClick', path });