From 9961d59c7820ba30cb6058c7dff57a324be93463 Mon Sep 17 00:00:00 2001 From: Octopus Date: Tue, 7 Apr 2026 10:30:35 +0800 Subject: [PATCH] fix: read VITE_API_BASE_URL from .env for Vite dev proxy (fixes #480) The Vite development proxy target was hardcoded to http://localhost:5001, ignoring the VITE_API_BASE_URL environment variable that the axios client already respects. This caused the dev proxy to still route to localhost even when users configured a remote or custom backend address. - Use loadEnv() in vite.config.js to read VITE_API_BASE_URL from the project root .env file and pass it as the proxy target (fallback: localhost:5001) - Document VITE_API_BASE_URL in .env.example so users know the variable exists --- .env.example | 8 +++++++- frontend/vite.config.js | 39 ++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index 78a3b72c0..b6f383d3f 100644 --- a/.env.example +++ b/.env.example @@ -13,4 +13,10 @@ ZEP_API_KEY=your_zep_api_key_here # 注意如果不使用加速配置,env文件中就不要出现下面的配置项 LLM_BOOST_API_KEY=your_api_key_here LLM_BOOST_BASE_URL=your_base_url_here -LLM_BOOST_MODEL_NAME=your_model_name_here \ No newline at end of file +LLM_BOOST_MODEL_NAME=your_model_name_here + +# ===== 前端 / 部署配置(可选)===== +# 后端 API 地址(axios 客户端 + Vite 开发代理均读取此变量) +# 默认值:http://localhost:5001 +# 若后端运行在其他主机或端口,请取消注释并修改为对应地址 +# VITE_API_BASE_URL=http://localhost:5001 \ No newline at end of file diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 8f1e4c11b..b2b9889ba 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -1,24 +1,29 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' // https://vite.dev/config/ -export default defineConfig({ - plugins: [vue()], - resolve: { - alias: { - '@': path.resolve(__dirname, 'src'), - '@locales': path.resolve(__dirname, '../locales') - } - }, - server: { - port: 3000, - open: true, - proxy: { - '/api': { - target: 'http://localhost:5001', - changeOrigin: true, - secure: false +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, path.resolve(__dirname, '..'), '') + const backendUrl = env.VITE_API_BASE_URL || 'http://localhost:5001' + + return { + plugins: [vue()], + resolve: { + alias: { + '@': path.resolve(__dirname, 'src'), + '@locales': path.resolve(__dirname, '../locales') + } + }, + server: { + port: 3000, + open: true, + proxy: { + '/api': { + target: backendUrl, + changeOrigin: true, + secure: false + } } } }