fix: VITE_API_BASE 尾部斜杠剥除——'/' 拼出 '//api/...' 协议相对 URL 指向不存在的主机 api

This commit is contained in:
Sisyphus committed 2026-08-25 23:50:51 +08:00
1 parent ab3da5a730
commit 48e58257e9
2 files changed
+12 -7

No files matched your search

+7 -5
View File
@@ -21,12 +21,14 @@ COPY tsconfig.json vite.config.ts index.html ./
COPY src ./src
COPY public ./public
# 生产 API 基址:设为 "/",这样 request() 拼出的路径如 "//api/auth/login"
# 浏览器自动规范化为 "/api/auth/login",再由 Nginx "/api/" 位置反代到后端 :8788。
# 若设为空字符串则路径无变化,但 "/" 更语义明确。
# 注意:仅影响本镜像构建;本地 `npm run dev`(Vite 5173)无此变量,仍回退到
# 生产 API 基址:留空(同源相对路径),request() 拼出 "/api/auth/login",
# 由 Nginx "/api/" 位置反代到后端 :8788。
# 注意不要设为 "/":那会拼出 "//api/auth/login"——协议相对 URL 会被浏览器
# 解析成 http://api/auth/login(主机名 "api"),所有请求网络层失败。
# (api.ts 现在也会剥掉尾部斜杠,双保险。)
# 仅影响本镜像构建;本地 `npm run dev`(Vite 5173)无此变量,仍回退到
# src/lib/api.ts 中的开发默认值 http://localhost:8788。
ARG VITE_API_BASE=/
ARG VITE_API_BASE=
ENV VITE_API_BASE=$VITE_API_BASE
# 构建 React 应用 → dist/(与 package.json 的 build 脚本一致:vite build)
+5 -2
View File
@@ -83,8 +83,11 @@ export interface GenerateChibiResponse {
downloadGrant: string;
}
/** Base URL: override with VITE_API_BASE, default to the local dev server. */
export const API_BASE: string = import.meta.env.VITE_API_BASE ?? 'http://localhost:8788';
/** Base URL: override with VITE_API_BASE, default to the local dev server.
* 尾部斜杠一律剥掉:'/' 会拼出 '//api/...' 这种协议相对 URL,
* 浏览器解析成 http://api/...(主机名 "api")导致所有请求网络层失败。 */
const RAW_API_BASE: string = import.meta.env.VITE_API_BASE ?? 'http://localhost:8788';
export const API_BASE: string = RAW_API_BASE.replace(/\/+$/, '');
/**
* Shared JSON helper: normalizes headers, then parses the body.