-
Couldn't load subscription status.
- Fork 1.2k
fix the websocket connection error #378
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?
Conversation
Expose SERVER_BASE_URL in the environment variables.
Set SERVER_BASE_URL environment variable for client bundle.
Summary of ChangesHello @hass-demacia, 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 resolves a websocket connection error by correctly exposing the Highlights
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 attempts to fix a WebSocket connection issue by exposing SERVER_BASE_URL to the client. While the intent is correct, the implementation hardcodes http://localhost:8001 as the server URL in the client-side bundle. This is a critical flaw that will prevent the application from working in any deployed environment outside of a local machine. My review provides detailed feedback on why this is an issue and suggests a more robust, dynamic approach where the client constructs the server URL based on its own location, which is standard practice for web applications.
| ENV NODE_OPTIONS="--max-old-space-size=4096" | ||
| ENV NEXT_TELEMETRY_DISABLED=1 | ||
| # `next build` inject `SERVER_BASE_URL` into bundle.js for client, once code is builded, bundle can't be modify by .env or ENV parameters. | ||
| ENV SERVER_BASE_URL="http://localhost:${PORT:-8001}" |
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.
Setting SERVER_BASE_URL here hardcodes localhost into the build artifact. This value is then passed to the client-side code via the next.config.ts change, which will cause connection failures in any non-local deployment. The client's browser would try to connect to its own localhost instead of your server.
This environment variable should be removed from the build stage. The client should determine the server URL dynamically.
| env: { | ||
| SERVER_BASE_URL: TARGET_SERVER_BASE_URL, | ||
| }, |
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.
Exposing TARGET_SERVER_BASE_URL to the client bundle is problematic because it will contain a localhost URL baked in at build time. This will cause connection errors for users in a deployed environment.
For WebSocket connections on the client side, it's better to construct the URL dynamically based on window.location. This avoids hardcoding hostnames. For example:
// In client-side code
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsHost = window.location.host;
const wsUrl = `${wsProtocol}//${wsHost}`; // Adjust path as neededThis assumes your reverse proxy is configured to route WebSocket traffic from the same host to your backend service.
I recommend removing this env block. The SERVER_BASE_URL environment variable should be used for server-side logic only (like the existing rewrites), where it can be configured at runtime.
#132
Next.js doesn't expose parameters into bundle.js default.
By adding the NEXT_PUBLIC_ prefix to an environment variable, it will be exposed to the browser.
Or adding the parameter in the
next.config.js.next.config.js environment-variables