diff --git a/perler-lab/Dockerfile b/perler-lab/Dockerfile index 9b322a9..273cbb5 100644 --- a/perler-lab/Dockerfile +++ b/perler-lab/Dockerfile @@ -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) diff --git a/perler-lab/src/lib/api.ts b/perler-lab/src/lib/api.ts index 0f79a0f..da0d6cc 100644 --- a/perler-lab/src/lib/api.ts +++ b/perler-lab/src/lib/api.ts @@ -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.