Skip to content

fix(electron): 修复开发环境下的本地主机地址配置问题#1027

Open
laoshuikaixue wants to merge 1 commit intoimsyy:devfrom
laoshuikaixue:fix/electron-dev-host-address
Open

fix(electron): 修复开发环境下的本地主机地址配置问题#1027
laoshuikaixue wants to merge 1 commit intoimsyy:devfrom
laoshuikaixue:fix/electron-dev-host-address

Conversation

@laoshuikaixue
Copy link
Copy Markdown
Contributor

  • 将 localhost 替换为 127.0.0.1 以解决网络连接问题
  • 统一所有窗口 URL 配置中的地址引用
  • 在 Vite 配置中设置服务器主机地址为 127.0.0.1
  • 创建专门的 devRendererUrl 变量处理开发环境渲染器地址
  • 确保歌词窗口和加载窗口使用正确的本地地址
  • 修复了 ELECTRON_RENDERER_URL 环境变量的地址替换逻辑

- 将 localhost 替换为 127.0.0.1 以解决网络连接问题
- 统一所有窗口 URL 配置中的地址引用
- 在 Vite 配置中设置服务器主机地址为 127.0.0.1
- 创建专门的 devRendererUrl 变量处理开发环境渲染器地址
- 确保歌词窗口和加载窗口使用正确的本地地址
- 修复了 ELECTRON_RENDERER_URL 环境变量的地址替换逻辑
@laoshuikaixue
Copy link
Copy Markdown
Contributor Author

laoshuikaixue commented Apr 5, 2026

image 修复了开启Clash TUN模式的严格路由,遇到的如下报错 image
20:21:50.548 (default)  > (node:8948) electron: Failed to load URL: http://localhost:14558/web/loading/index.html with error: ERR_CONNECTION_REFUSED
(Use `electron --trace-warnings ...` to show where the warning was created)
20:21:50.598 (default)  > (node:8948) electron: Failed to load URL: http://localhost:14558/ with error: ERR_CONNECTION_REFUSED

@laoshuikaixue
Copy link
Copy Markdown
Contributor Author

@gemini-code-assist review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 updates the development and preview server configurations to use 127.0.0.1 instead of localhost and adjusts the URL generation logic in the Electron main process. Review feedback suggests refining the fallback logic for the renderer URL by incorporating a dedicated web port and refactoring the URL constants to eliminate redundancy.

Comment on lines +35 to +38
const devRendererUrl = process.env["ELECTRON_RENDERER_URL"]?.replace(
"://localhost",
"://127.0.0.1",
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议同时引入渲染进程的开发端口 VITE_WEB_PORT,以便在 ELECTRON_RENDERER_URL 环境变量缺失时,能为开发环境提供更准确的回退地址。在进行字符串替换时,遵循项目惯例使用简单的 string.replace 方式。

export const webPort = Number(import.meta.env["VITE_WEB_PORT"] || 14558);

const devRendererUrl = process.env["ELECTRON_RENDERER_URL"]?.replace(
  "://localhost",
  "://127.0.0.1",
);
References
  1. When modifying existing code, retain simple and clear implementations (like string.replace) over more complex, robust alternatives (like regex) if the original implementation is sufficient.

Comment on lines 50 to +69
export const mainWinUrl =
isDev && process.env["ELECTRON_RENDERER_URL"]
? process.env["ELECTRON_RENDERER_URL"]
: `http://localhost:${port}`;
isDev && devRendererUrl ? devRendererUrl : `http://127.0.0.1:${port}`;

/**
* 歌词窗口加载地址
* @returns string
*/
export const lyricWinUrl =
isDev && process.env["ELECTRON_RENDERER_URL"]
? `${process.env["ELECTRON_RENDERER_URL"]}/#/desktop-lyric`
: `http://localhost:${port}/#/desktop-lyric`;
isDev && devRendererUrl
? `${devRendererUrl}/#/desktop-lyric`
: `http://127.0.0.1:${port}/#/desktop-lyric`;

/**
* 加载窗口地址
* @returns string
*/
export const loadWinUrl =
isDev && process.env["ELECTRON_RENDERER_URL"]
? `${process.env["ELECTRON_RENDERER_URL"]}/web/loading/index.html`
: `http://localhost:${port}/web/loading/index.html`;
isDev && devRendererUrl
? `${devRendererUrl}/web/loading/index.html`
: `http://127.0.0.1:${port}/web/loading/index.html`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议利用 mainWinUrl 作为基础路径来简化 lyricWinUrl 和 loadWinUrl 的定义,减少重复代码。此外,在开发环境下(isDev 为 true)如果 devRendererUrl 缺失,应回退到渲染进程端口(webPort)而非后端服务端口(port)。

export const mainWinUrl =
  isDev ? (devRendererUrl || "http://127.0.0.1:" + webPort) : "http://127.0.0.1:" + port;

/**
 * 歌词窗口加载地址
 * @returns string
 */
export const lyricWinUrl = mainWinUrl + "/#/desktop-lyric";

/**
 * 加载窗口地址
 * @returns string
 */
export const loadWinUrl = mainWinUrl + "/web/loading/index.html";

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 updates the application configuration to use the explicit IP address 127.0.0.1 instead of localhost. Specifically, it modifies the Vite server and preview host settings in electron.vite.config.ts and updates the URL generation logic for the main, lyric, and loading windows in electron/main/utils/config.ts. I have no feedback to provide.

@laoshuikaixue
Copy link
Copy Markdown
Contributor Author

laoshuikaixue commented Apr 5, 2026

Code Review

This pull request updates the application configuration to use the explicit IP address 127.0.0.1 instead of localhost. Specifically, it modifies the Vite server and preview host settings in electron.vite.config.ts and updates the URL generation logic for the main, lyric, and loading windows in electron/main/utils/config.ts. I have no feedback to provide.

?左右脑互搏 怎么又没了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant