From 756849f99ef9b9ff58692d5603e57cdf4377849d Mon Sep 17 00:00:00 2001 From: selfrelease Date: Thu, 25 Jun 2026 21:18:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=AF=B9=20NEXT=5FPUBLIC=5FAPI=5FURL=20=E7=9A=84?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - API_BASE 客户端使用空字符串(same-origin 通过 nginx) - getStreamBase 服务端改用 API_URL - getPPTDownloadURL 客户端使用 same-origin - 彻底解决生产环境 CORS 错误 --- apps/web/src/lib/api.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts index ba6c152..77109c3 100644 --- a/apps/web/src/lib/api.ts +++ b/apps/web/src/lib/api.ts @@ -1,7 +1,9 @@ +// 服务端 SSR:使用内网地址直连后端 +// 客户端:使用空字符串(same-origin,通过 nginx 代理) const API_BASE = typeof window === "undefined" - ? process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080" - : process.env.NEXT_PUBLIC_API_URL || ""; + ? process.env.API_URL || "http://localhost:8080" + : ""; // 流式请求基础URL(运行时判断,不依赖编译时变量): // - 服务器端(SSR):直连后端 @@ -9,7 +11,7 @@ const API_BASE = // - 浏览器端生产环境:走同域nginx(已配proxy_buffering off) function getStreamBase() { if (typeof window === "undefined") { - return process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080"; + return process.env.API_URL || "http://localhost:8080"; } const host = window.location.hostname; if (host === "localhost" || host === "127.0.0.1") { @@ -234,7 +236,8 @@ export async function listPPTTasks() { } export function getPPTDownloadURL(taskId: string) { - const base = typeof window !== "undefined" ? process.env.NEXT_PUBLIC_API_URL || "" : ""; + // 客户端使用 same-origin,服务端不应调用此函数 + const base = typeof window !== "undefined" ? "" : ""; return `${base}/api/v1/ppt/tasks/${taskId}/download`; }