From 1e3451b05814d612c0864013c73f6bcd5b410f04 Mon Sep 17 00:00:00 2001 From: JasonOA888 Date: Mon, 9 Mar 2026 16:18:22 +0800 Subject: [PATCH] fix(frontend): use relative baseURL in production, avoid hardcoded localhost Previously baseURL defaulted to 'http://localhost:5001' in all environments, causing issues when deployed on servers or Docker with port mapping. Changes: - Use relative path (empty string) in production for same-origin deployment - Keep localhost:5001 default for development environment - Still respects VITE_API_BASE_URL env var if set Fixes #93, related to #59, #57 --- frontend/src/api/index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index e2d9465b..1565657a 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -1,8 +1,21 @@ import axios from 'axios' // 创建axios实例 +// 优先级: VITE_API_BASE_URL > 生产环境相对路径 > 开发环境localhost +const getBaseURL = () => { + if (import.meta.env.VITE_API_BASE_URL) { + return import.meta.env.VITE_API_BASE_URL + } + // 生产环境使用相对路径(同源部署) + if (import.meta.env.PROD) { + return '' // 相对路径,使用当前域名 + } + // 开发环境默认localhost + return 'http://localhost:5001' +} + const service = axios.create({ - baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5001', + baseURL: getBaseURL(), timeout: 300000, // 5分钟超时(本体生成可能需要较长时间) headers: { 'Content-Type': 'application/json'