From e80b170fd1e010bfa98a129ece1c07b333454e45 Mon Sep 17 00:00:00 2001 From: Lotus CI Date: Wed, 23 Sep 2026 22:46:05 -0400 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA --- src/index.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index f03746241..63631bc7d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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) => {