From 4ca58750fbec510e94fffb1fb4f4d6538a09fa75 Mon Sep 17 00:00:00 2001 From: itsmylife44 <34112129+itsmylife44@users.noreply.github.com> Date: Fri, 30 Jan 2026 18:11:53 +0100 Subject: [PATCH] fix(ui): prevent Vite tree-shaking of web mode API URL detection When building the UI for Docker deployment, Vite's tree-shaking removes the web mode detection branch because it can statically evaluate `!window.electron` as `true` during build (window.electron is undefined in the build environment). This causes the production bundle to always use `http://localhost:3008` as the API URL, which fails in Docker web deployments where the browser cannot reach localhost on the server. The fix uses the `in` operator which Vite cannot statically analyze: ```typescript // Before: Vite optimizes this away if (!window.electron) { return ''; } // After: Vite cannot determine result at build time const isElectron = 'electron' in window && window.electron != null; if (!isElectron) { return ''; } ``` In web mode, this returns an empty string, causing fetch requests to use relative URLs (`/api/...`) which nginx can proxy to the backend server. --- apps/ui/src/lib/http-api-client.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/ui/src/lib/http-api-client.ts b/apps/ui/src/lib/http-api-client.ts index dbfddc4cc..cb9dc2361 100644 --- a/apps/ui/src/lib/http-api-client.ts +++ b/apps/ui/src/lib/http-api-client.ts @@ -157,9 +157,12 @@ const getServerUrl = (): string => { const envUrl = import.meta.env.VITE_SERVER_URL; if (envUrl) return envUrl; - // In web mode (not Electron), use relative URL to leverage Vite proxy + // In web mode (not Electron), use relative URL to leverage nginx proxy // This avoids CORS issues since requests appear same-origin - if (!window.electron) { + // Note: Using 'in' operator prevents Vite from tree-shaking this branch, + // as it cannot statically determine the result at build time + const isElectron = 'electron' in window && window.electron != null; + if (!isElectron) { return ''; } }