-
Notifications
You must be signed in to change notification settings - Fork 524
fix(ui): prevent Vite tree-shaking of web mode API URL detection #731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(ui): prevent Vite tree-shaking of web mode API URL detection #731
Conversation
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.
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @itsmylife44, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request provides a critical fix for Docker web deployments where the UI was unable to connect to the backend API due to Cross-Origin Request Blocked (CORS) errors. The issue stemmed from Vite's tree-shaking mechanism, which incorrectly optimized away the web mode detection logic during the build process. This resulted in the UI attempting to connect to Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses a critical bug where Vite's tree-shaking was incorrectly removing code required for web deployments in Docker. The fix, which involves using the in operator to prevent static analysis at build time, is a clever and effective solution to ensure the runtime check for the Electron environment is preserved. The code change is well-contained and the accompanying comments clearly explain the rationale. This is a good fix that should resolve the CORS errors seen in Docker web deployments.
Summary
Fixes Docker web deployments where the UI cannot connect to the backend API.
Problem
When accessing AutoMaker via browser in Docker deployment, all API requests fail with CORS errors:
The UI is trying to reach
localhost:3008which, from the user's browser, refers to their local machine - not the Docker container.Root Cause
The
getServerUrl()function has web mode detection:However, Vite's tree-shaking evaluates
!window.electronastrueduring build (sincewindow.electronis undefined in the build environment) and removes this branch entirely. The production bundle becomes:Solution
Use the
inoperator which Vite cannot statically analyze:This preserves the runtime check in the production bundle, allowing web mode to use relative URLs (
/api/...) that nginx can proxy to the backend.Docker Deployment Note
For Docker web deployments to work, nginx must proxy
/api/requests to the backend. The UI'sdocker-compose.ymlshould include nginx configuration like:Changes
apps/ui/src/lib/http-api-client.ts: Useinoperator for Electron detectionTesting