fix(electron): disable service worker in desktop runtime

This commit is contained in:
4gray committed 2026-06-14 15:59:03 +02:00
1 parent 8739e95b70
commit ce78669c17
5 files changed
+201 -7

No files matched your search

@@ -36,4 +36,42 @@ describe('runtime config helpers', () => {
).toBe(false);
expect(shouldEnableServiceWorker(true, {} as Navigator)).toBe(false);
});
it('disables service worker for Electron runtime', () => {
expect(
shouldEnableServiceWorker(
true,
{ serviceWorker: {} } as Navigator,
{
electronBridge: {},
protocol: 'file:',
}
)
).toBe(false);
});
it('disables service worker for Electron runtime on non-file origins', () => {
expect(
shouldEnableServiceWorker(
true,
{ serviceWorker: {} } as Navigator,
{
electronBridge: {},
protocol: 'https:',
}
)
).toBe(false);
});
it('disables service worker for file origins without an Electron bridge', () => {
expect(
shouldEnableServiceWorker(
true,
{ serviceWorker: {} } as Navigator,
{
protocol: 'file:',
}
)
).toBe(false);
});
});
+26 -2
View File
@@ -19,9 +19,33 @@ export function getRuntimeBackendUrl(): string {
);
}
export interface ServiceWorkerRuntimeContext {
readonly electronBridge?: unknown;
readonly protocol?: string;
}
function getDefaultServiceWorkerRuntimeContext(): ServiceWorkerRuntimeContext {
const browserWindow = globalThis.window as
| (Window & { electron?: unknown })
| undefined;
return {
electronBridge: browserWindow?.electron,
protocol:
browserWindow?.location?.protocol ?? globalThis.location?.protocol,
};
}
export function shouldEnableServiceWorker(
production = AppConfig.production,
navigatorRef: Navigator | undefined = globalThis.navigator
navigatorRef: Navigator | undefined = globalThis.navigator,
runtimeContext = getDefaultServiceWorkerRuntimeContext()
): boolean {
return production && !!navigatorRef && 'serviceWorker' in navigatorRef;
return (
production &&
!!navigatorRef &&
'serviceWorker' in navigatorRef &&
!runtimeContext.electronBridge &&
runtimeContext.protocol !== 'file:'
);
}