fix: prevent 401 media errors from stale/missing SW session

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
This commit is contained in:
Evie Gauthier committed 2026-03-24 10:14:26 -04:00
1 parent 8f46cff25e
commit d93f4174b2
1 file changed
+15 -5
+15 -5
View File
@@ -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 },