fix: use absolute backend URL in tauri production mode (welcome page stuck on 'Loading Failed') - #307
Merged
Conversation
getApiBase() returned relative '/api' whenever window.location.host was truthy, which is always the case under tauri://localhost. In tauri production the webview cannot reach '/api' (tauri does not proxy it to the axum web server), so interface loading failed with '加载失败' (welcome page stuck) on Linux. Fix: once the backend port has been discovered (setBackendPort), prefer the absolute http://host:port/api URL. The relative path remains as a dev-mode (Vite proxy) fallback. Same fix applied to getWsUrl() for the WebSocket endpoint. Fixes MaaEnd/MaaEnd#4692 (reported against MaaEnd v2.22.0 on Arch Linux + Hyprland, tauri webview stuck on welcome page 'Loading Failed').
Contributor
There was a problem hiding this comment.
Hey - 我在这里给出了一些总体反馈:
- 在
getApiBase和getWsUrl中用于推导协议/主机名的逻辑是重复的,并且与 Nginx/host 分支存在一些差异;建议把 URL 构建集中到一个辅助方法中,以保持行为一致并更易于维护。 - 在
getApiBase中,backendPort > 0分支即使在同源反向代理后面运行时,也会始终优先选择直连 URL;如果这是刻意设计的,可能值得显式防范hostname与后端绑定不匹配的情况(例如,当探测到的端口只监听在 localhost 时强制使用localhost)。
给 AI Agent 的提示
请根据这次代码评审中的评论进行修改:
## 总体评论
- 在 `getApiBase` 和 `getWsUrl` 中用于推导协议/主机名的逻辑是重复的,并且与 Nginx/host 分支存在一些差异;建议把 URL 构建集中到一个辅助方法中,以保持行为一致并更易于维护。
- 在 `getApiBase` 中,`backendPort > 0` 分支即使在同源反向代理后面运行时,也会始终优先选择直连 URL;如果这是刻意设计的,可能值得显式防范 `hostname` 与后端绑定不匹配的情况(例如,当探测到的端口只监听在 localhost 时强制使用 `localhost`)。帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English
Hey - I've left some high level feedback:
- The logic for deriving protocol/hostname in both
getApiBaseandgetWsUrlis duplicated and slightly different from the Nginx/host branch; consider centralizing URL construction in a helper to keep behavior consistent and easier to maintain. - In
getApiBase, thebackendPort > 0branch always prefers a direct URL even when running behind a same-origin reverse proxy; if that’s intentional, it may be worth explicitly guarding against mismatchedhostname/backend binding (e.g., forcinglocalhostwhen the probed port only listens there).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The logic for deriving protocol/hostname in both `getApiBase` and `getWsUrl` is duplicated and slightly different from the Nginx/host branch; consider centralizing URL construction in a helper to keep behavior consistent and easier to maintain.
- In `getApiBase`, the `backendPort > 0` branch always prefers a direct URL even when running behind a same-origin reverse proxy; if that’s intentional, it may be worth explicitly guarding against mismatched `hostname`/backend binding (e.g., forcing `localhost` when the probed port only listens there).Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
In tauri mode, interface.json loads via invoke (successful), but all subsequent API calls (getApiBase()/config, /background-image, /local-file) went to the relative /api path because backendPort was never set in the tauri branch — only the browser-env port probe sets it. Under tauri://localhost the relative /api resolves to a non-existent endpoint -> 'Could not connect to localhost'. Set backendPort from the actual web server port (invoke get_web_server_port) in the tauri branch, falling back to 12701.
In tauri production the frontend is served from tauri://localhost (embedded assets) but fetches the axum backend at http://127.0.0.1:12701 — a cross-origin request. The production CorsLayer omitted allow_origin, so the browser blocked every API call, surfacing as 'Could not connect to localhost: Connection refused'. Match the debug config (allow_origin Any + allow_headers Any).
get_web_server_port returns 0 (no error) when the web server is not yet bound or disabled. The previous fix only set backendPort when port > 0, so backendPort stayed 0 during startup races and getApiBase() fell back to the relative /api path. Set backendPort unconditionally, defaulting to 12701.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题 / Problem
On Linux (Arch + Hyprland, tauri webview), the welcome page is stuck on a "Loading Failed / 加载失败" error dialog and the main UI is unreachable. Reported as MaaEnd/MaaEnd#4692.
Root cause:
getApiBase()returns the relative path/apiwheneverwindow.location.hostis truthy — which is always the case undertauri://localhost. In tauri production the webview cannot reach/api(tauri does not proxy it to the axum web server), so the interface fetch fails and content loading (DESCRIPTION.mdetc.) falls back to showing the raw path with the error dialog.修复 / Fix
getApiBase(): once the backend port has been discovered viasetBackendPort(), prefer the absolutehttp://host:port/apiURL. The relative/apipath remains as a dev-mode (Vite proxy) / same-origin reverse-proxy fallback.getWsUrl(): same fix for the WebSocket endpoint (serverPortalready exists).Verified:
pnpm buildpasses; the compiledgetApiBasenow returns the absolute URL when the backend port is known.验证 / Verification
curl http://127.0.0.1:12701/api/interfaceand/api/local-file?path=...both return 200)./apipath resolves undertauri://localhostand returns the embeddedindex.html(SPA fallback) instead of JSON.http://localhost:12701/api/...which works.Summary by Sourcery
确保在探测到后端端口可用时,前端使用绝对后端 URL,以修复 Tauri 生产模式下的 API 加载失败问题。
New Features:
httpURL 直接连接到后端。Bug Fixes:
/api请求,修复欢迎页加载失败的问题。wsURL 修复 WebSocket 连接问题。Enhancements:
Original summary in English
Summary by Sourcery
Ensure frontend uses an absolute backend URL when a probed backend port is available to fix API loading failures in Tauri production mode.
New Features:
Bug Fixes:
Enhancements: