From c0b91e4226cd9ca5c29d594ae0af482221f697ec Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Tue, 15 Aug 2023 18:05:41 +0200 Subject: [PATCH] Fix update-worker breakage caused by use of Window in desktop API code Window is not defined in service workers. The service worker code does import service-versions though, which in turn imports desktop-apis. This is a quick fix for this issue, avoiding directly depending on 'window' which results in the update-worker failing to run. It would be better to avoid loading this file entirely, using a dynamic import or something more thorough, but Webpack v4 seems to fail to handle dynamic imports in this case, so that's not possible for now. It would be even better to solve this more structurally - e.g. checking the service worker actually installs OK in a test - but that's hard and this fixes the immediate production issue for now. --- src/services/desktop-api.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/services/desktop-api.ts b/src/services/desktop-api.ts index 708b992b..66ddeed4 100644 --- a/src/services/desktop-api.ts +++ b/src/services/desktop-api.ts @@ -56,4 +56,12 @@ interface NativeContextMenuSubmenu { items: readonly NativeContextMenuItem[]; } -export const DesktopApi: DesktopApi = window.desktopApi ?? {}; \ No newline at end of file +// Quick fix to avoid this file crashing the update SW which doesn't have 'window' available, without +// also breaking old Electron that doesn't have globalThis: +const global = typeof globalThis !== 'undefined' + ? globalThis + : typeof window !== 'undefined' + ? window + : {}; + +export const DesktopApi: DesktopApi = global.desktopApi ?? {}; \ No newline at end of file