fix(sw): only register the service worker on http(s) pages

register() rejects on non-http(s) origins, and the rejection was
unhandled. The desktop app's debug build loads from tauri://localhost,
which surfaced as a Sentry "serviceWorker.register() must be called with
a script URL whose protocol is either HTTP or HTTPS". Skip registration
there, and catch any other failure (e.g. SWs disabled) with a warning.
The app works without a SW.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
This commit is contained in:
Lotus CI
2026-09-23 22:46:05 -04:00
co-authored by Claude Opus 5.5
parent 76929a8763
commit e80b170fd1
+13 -2
View File
@@ -22,7 +22,14 @@ import { cleanupSearchCacheIfSignedOut } from './client/initMatrix';
document.body.classList.add(configClass, varsClass);
// Register Service Worker
if ('serviceWorker' in navigator) {
// Service workers only register on http(s) pages. The desktop app loads from
// `tauri://localhost` in debug builds (and on any platform where the localhost
// plugin isn't used), where register() rejects: that surfaced in Sentry as an
// unhandled "must be called with a script URL whose protocol is either HTTP or
// HTTPS". Skip it there; the app works without a SW, only authenticated media
// falls back to the client's own fetch.
const swProtocolOk = window.location.protocol === 'https:' || window.location.protocol === 'http:';
if ('serviceWorker' in navigator && swProtocolOk) {
const swUrl =
import.meta.env.MODE === 'production'
? `${trimTrailingSlash(import.meta.env.BASE_URL)}/sw.js`
@@ -39,7 +46,11 @@ if ('serviceWorker' in navigator) {
// never route. Production sw.js is a classic bundled script.
navigator.serviceWorker
.register(swUrl, import.meta.env.MODE === 'production' ? undefined : { type: 'module' })
.then(sendSessionToSW);
.then(sendSessionToSW)
.catch((err) => {
// e.g. a private window with SWs disabled; the app still works.
console.warn('Service worker registration failed', err);
});
navigator.serviceWorker.ready.then(sendSessionToSW);
navigator.serviceWorker.addEventListener('message', (ev) => {