Skip to content

fix: deliver session to service worker on first load (authenticated media)#2991

Open
qingyun-wu wants to merge 1 commit into
cinnyapp:devfrom
qingyun-wu:fix/authed-media-first-load
Open

fix: deliver session to service worker on first load (authenticated media)#2991
qingyun-wu wants to merge 1 commit into
cinnyapp:devfrom
qingyun-wu:fix/authed-media-first-load

Conversation

@qingyun-wu

Copy link
Copy Markdown

Problem

With authenticated media enabled (MSC3916; mandatory on Synapse ≥ 1.131), images/media fail with 401 M_MISSING_TOKEN on a fresh load (first visit, hard refresh, incognito) and only work after a second reload.

pushSessionToSW posts the access token to the media service worker only when navigator.serviceWorker.controller is set. On first load the controller is null until the SW claims the page, so the token never reaches the SW before the first media fetches go out → 401 → broken images.

Closes #2990.

Fix

  • src/sw-session.ts: when controller is null, fall back to navigator.serviceWorker.ready → registration.active so the session reaches the SW on first load.
  • src/index.tsx: also re-push the session on controllerchange, once the SW takes control.

Two tiny changes, using existing patterns; no behavior change once the SW is already controlling the page.

Testing

Verified on a Synapse 1.153 deployment with authenticated media enforced: before, first-load media 401'd until reload; after, media loads on first load (incognito + unregistered-SW tested). navigator.serviceWorker.controller is null on that first load, which is the window this fixes.

…edia)

Under authenticated media (MSC3916, mandatory on Synapse >=1.131), images
fail with 401 on a fresh load until a second reload. pushSessionToSW only
posts the access token when navigator.serviceWorker.controller is set, but
on first load the controller is null until the SW claims the page, so the
token never reaches the SW before the first media fetches fire.

- sw-session: when controller is null, fall back to serviceWorker.ready ->
  registration.active so the token reaches the SW on first load.
- index: re-push the session on 'controllerchange' once the SW takes control.

Fixes cinnyapp#2990.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authenticated media (MSC3916): media fails on first load until a second reload (SW token-push timing)

2 participants