From d93f4174b2bb1d54cd7499e1fab98a9901cc87bb Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Sat, 21 Mar 2026 21:46:39 -0400 Subject: [PATCH] fix: prevent 401 media errors from stale/missing SW session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three changes to src/sw.ts: 1. Fix early-return bug in fetch handler: when sessions.get(clientId) returns a session but validMediaRequest(url, session.baseUrl) fails (e.g. multi-account users on different homeservers, or URL format mismatch), the old code returned without calling event.respondWith(), causing the browser to make an unauthenticated request → 401. Now we only short-circuit if BOTH conditions are met. 2. Proactively broadcast requestSession to all window clients in the activate event. After a SW restart the sessions Map is empty; the old code relied on the first intercepted media fetch to trigger requestSessionWithTimeout. Now the SW pre-populates the Map right after claiming clients, eliminating the 3-second race window. 3. Add loadPersistedSession() as a final fallback in the fetch handler. If requestSessionWithTimeout returns nothing ( Three changes to src/sw.ts: 1. Fix early-return bug in fetch handler: when sessions.get(clientId) returns a session buo a 1. Fix early-return bug i --- src/sw.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/sw.ts b/src/sw.ts index 62708191..944382d1 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -460,6 +460,11 @@ self.addEventListener('activate', (event: ExtendableEvent) => { (async () => { await self.clients.claim(); await cleanupDeadClients(); + // Proactively request sessions from all window clients so the sessions Map + // is pre-populated after a SW restart, rather than waiting for the first + // media fetch to trigger requestSessionWithTimeout. + const windowClients = await self.clients.matchAll({ type: 'window' }); + windowClients.forEach((client) => client.postMessage({ type: 'requestSession' })); })() ); }); @@ -589,10 +594,8 @@ self.addEventListener('fetch', (event: FetchEvent) => { const redirect: RequestRedirect = 'follow'; const session = sessions.get(clientId); - if (session) { - if (validMediaRequest(url, session.baseUrl)) { - event.respondWith(fetch(url, { ...fetchConfig(session.accessToken), redirect })); - } + if (session && validMediaRequest(url, session.baseUrl)) { + event.respondWith(fetch(url, { ...fetchConfig(session.accessToken), redirect })); return; } @@ -611,10 +614,17 @@ self.addEventListener('fetch', (event: FetchEvent) => { } event.respondWith( - requestSessionWithTimeout(clientId).then((s) => { + requestSessionWithTimeout(clientId).then(async (s) => { + // Primary: session received from the live client window. if (s && validMediaRequest(url, s.baseUrl)) { return fetch(url, { ...fetchConfig(s.accessToken), redirect }); } + // Fallback: try the persisted session (helps when SW restarts on iOS and + // the client window hasn't responded to requestSession yet). + const persisted = await loadPersistedSession(); + if (persisted && validMediaRequest(url, persisted.baseUrl)) { + return fetch(url, { ...fetchConfig(persisted.accessToken), redirect }); + } console.warn( '[SW fetch] No valid session for media request', { url, clientId, hasSession: !!s },