refactor(config): 统一后端地址的构建配置
移除运行时配置脚本,优先使用构建环境变量,未配置时使用同源地址。同步更新环境变量示例和部署说明。
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
# Backend root URL. Leave empty for same-origin requests (Vite proxy in development).
|
||||
# Restart the dev server or rebuild production assets after changing this value.
|
||||
# Public configuration only; never put secrets in VITE_* variables.
|
||||
VITE_REACT_APP_SERVER_URL=
|
||||
|
||||
# Frontend build-time feature flags. Rebuild the frontend after changing them.
|
||||
# Missing or false keeps the Cloud Brain suite hidden and blocks direct routes.
|
||||
VITE_CLOUD_BRAIN_UI_ENABLED=false
|
||||
|
||||
@@ -63,13 +63,7 @@ Copy-Item .env.example .env.local
|
||||
|
||||
### 推荐:浏览器同源请求,通过 Vite 代理后端
|
||||
|
||||
保持 [public/config.js](./public/config.js) 中 `SERVER_URL` 为空,也不设置 `VITE_REACT_APP_SERVER_URL`:
|
||||
|
||||
```js
|
||||
window.__APP_CONFIG__ = {
|
||||
SERVER_URL: '',
|
||||
};
|
||||
```
|
||||
不设置 `VITE_REACT_APP_SERVER_URL`(或设为空字符串),接口默认使用当前网页域名。
|
||||
|
||||
[vite.config.js](./vite.config.js) 将下列前缀转发到 `DEV_PROXY_TARGET` 指定的后端:
|
||||
|
||||
@@ -81,15 +75,7 @@ window.__APP_CONFIG__ = {
|
||||
|
||||
### 可选:浏览器直接访问独立后端域名
|
||||
|
||||
可以配置运行时文件:
|
||||
|
||||
```js
|
||||
window.__APP_CONFIG__ = {
|
||||
SERVER_URL: 'https://api.example.com',
|
||||
};
|
||||
```
|
||||
|
||||
也可以在 `.env.local` 中配置构建变量:
|
||||
在 `.env.local` 中配置构建变量:
|
||||
|
||||
```dotenv
|
||||
VITE_REACT_APP_SERVER_URL=https://api.example.com
|
||||
@@ -98,13 +84,12 @@ VITE_CLOUD_BRAIN_UI_ENABLED=false
|
||||
|
||||
接口根地址由 [src/helpers/config.js](./src/helpers/config.js) 决定,优先级为:
|
||||
|
||||
1. 非空的 `window.__APP_CONFIG__.SERVER_URL`。
|
||||
2. 非空的 `import.meta.env.VITE_REACT_APP_SERVER_URL`。
|
||||
3. 空字符串,使用当前网页域名。
|
||||
1. 非空的 `import.meta.env.VITE_REACT_APP_SERVER_URL`(构建变量)。
|
||||
2. 空字符串,使用当前网页域名(同源)。
|
||||
|
||||
地址填写后端根地址,不要额外添加 `/api`,业务请求会自己带上接口路径。直接连接独立域名时,请求不经过 Vite 的本地代理;后端需要正确配置 CORS、凭证和 Cookie 策略。公共 Axios 实例在根地址非空时启用 `withCredentials`。
|
||||
|
||||
`public/config.js` 在入口模块之前加载,构建后成为 `dist/config.js`。部署时修改这个文件并刷新页面即可切换运行时地址;环境变量则需要重启开发服务或重新构建。运行时地址为空时仍会回退到构建变量,不会强制覆盖为同源模式。
|
||||
已移除 `public/config.js` 及其入口脚本,不再支持部署后直接修改 `dist/config.js` 切换后端。旧部署如依赖该能力,需迁移为构建前设置 `VITE_REACT_APP_SERVER_URL`,或清空该变量后重新构建并配置同源反向代理。开发时修改变量需重启服务,生产时需重新构建并发布;仅修改部署服务器环境变量或刷新页面不会改变已有产物中的地址。
|
||||
|
||||
### 其他配置
|
||||
|
||||
@@ -117,13 +102,13 @@ VITE_CLOUD_BRAIN_UI_ENABLED=false
|
||||
|
||||
统计脚本插件使用 `process.env`,没有显式调用 Vite `loadEnv`;如果使用 Node/npm 运行 Vite,不要假定只写 `.env.local` 就能让配置文件读到这些统计变量,应由启动终端或 CI 环境注入。
|
||||
|
||||
前端环境变量和 `config.js` 都会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。
|
||||
前端构建变量会暴露给浏览器,仅放公开配置,不放上游模型密钥或后端机密。
|
||||
|
||||
## 4. 目录与代码入口
|
||||
|
||||
```text
|
||||
monkey-ai-web/
|
||||
├── public/ # 原样复制到构建目录的资源、logo、运行时 config.js
|
||||
├── public/ # 原样复制到构建目录的静态资源、logo
|
||||
├── scripts/ # 国际化提取、翻译整理等辅助脚本
|
||||
├── src/
|
||||
│ ├── index.jsx # React 挂载、语言初始化、全局 Provider
|
||||
@@ -142,7 +127,7 @@ monkey-ai-web/
|
||||
│ ├── styles/ # 主题 token、企业空间等样式
|
||||
│ └── docs/ # 随前端提供的静态文档内容
|
||||
├── tests/ # 部分 Node 测试,其他测试与源码相邻
|
||||
├── index.html # HTML 入口、启动占位画面、config.js 加载
|
||||
├── index.html # HTML 入口、启动占位画面、入口模块加载
|
||||
├── vite.config.js # 代理、别名、打包拆分
|
||||
├── tailwind.config.js # Tailwind 与品牌色变量映射
|
||||
├── package.json
|
||||
@@ -256,7 +241,7 @@ npm run preview
|
||||
- **路由回退**:本项目使用 BrowserRouter。直接访问 `/org/...` 等页面时,静态服务器需回退到 `index.html`,否则刷新会返回 404。
|
||||
- **接口转发**:生产环境配置实际的反向代理或独立后端地址。Vite 的开发代理不会打包进静态文件;`preview` 仅供本地预览,不能代替生产网关。
|
||||
- **实时响应**:使用 SSE 的接口需要关闭响应缓冲并设置合理超时;使用 WebSocket 时配置连接升级转发。
|
||||
- **配置缓存**:`config.js`、`index.html` 应允许及时更新,带哈希的静态资源可以使用长缓存。
|
||||
- **发布与缓存**:`index.html` 应允许及时更新,带哈希的静态资源可以使用长缓存。后端构建变量包含在产物中,切换地址需重新构建并发布配套 HTML 与资源,不再有独立 `config.js` 缓存。
|
||||
- **部署路径**:当前按域名根路径部署,资源和路由包含根路径引用。部署到子目录需另行检查 Vite `base`、路由和静态资源地址。
|
||||
|
||||
仓库中的 `vercel.json` 目前只有 GitHub 通知设置,没有配置 SPA 路由回退或后端代理,不能视为完整部署配置。
|
||||
@@ -268,11 +253,11 @@ npm run preview
|
||||
| 页面可打开,但提示无法加载状态或接口失败 | 后端是否可用;Network 中 `/api/status` 的目标地址与响应;Vite 代理是否正确 |
|
||||
| 登录后仍回到登录页 | `/api/user/self`、Cookie 是否发送、跨域凭证策略,以及测试账号状态 |
|
||||
| 企业数据为空或串到其他企业 | 当前组织选择、`current_org_id` 与请求中的 `X-Org-Id` |
|
||||
| 修改环境变量后无变化 | 是否重启开发服务或重新构建;`config.js` 是否覆盖环境变量;浏览器缓存 |
|
||||
| 修改环境变量后无变化 | 是否重启开发服务或重新构建并发布;构建时的变量值;HTML 与静态资源缓存 |
|
||||
| 云脑相关菜单找不到 | `VITE_CLOUD_BRAIN_UI_ENABLED`、后台模块开关和当前账号权限 |
|
||||
| 线上子页面刷新 404 | 静态服务器是否设置 SPA 回退,且接口路径没有误回退到 HTML |
|
||||
| 对话内容等到结束才一次性显示 | SSE 响应、反向代理缓冲和超时设置 |
|
||||
| 格式检查提示大量 Code style issues | 全仓仍有历史格式差异;保留统一配置,只格式化本次修改文件 |
|
||||
| 修改品牌后仍看到旧 Logo 或颜色 | 后端品牌设置、主题本地缓存以及 `config.js` / 静态资源缓存 |
|
||||
| 修改品牌后仍看到旧 Logo 或颜色 | 后端品牌设置、主题本地缓存以及 HTML / 静态资源缓存 |
|
||||
|
||||
首次联调建议依次确认:开发服务启动 → `/api/status` 正常 → 登录与 `/api/user/self` 正常 → 业务页面读取数据 → 选择已配置的测试模型验证对话。接口字段和业务权限问题需与后端负责人共同确认。
|
||||
|
||||
@@ -158,7 +158,7 @@ Monkey AI Web 是一个面向 AI 服务的 Web 前端,覆盖个人使用、企
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[浏览器] --> B[index.html / config.js]
|
||||
A[浏览器] --> B[index.html]
|
||||
B --> C[index.jsx:语言初始化与全局 Provider]
|
||||
C --> D[PageLayout:全局布局、站点状态、会话校验]
|
||||
D --> E[App.jsx:顶层路由与访问控制]
|
||||
@@ -170,7 +170,7 @@ flowchart TD
|
||||
J --> G
|
||||
```
|
||||
|
||||
开发环境中,浏览器可以通过 Vite 代理访问后端;生产环境则由部署网关转发接口,或通过运行时配置连接独立后端域名。后端内部如何划分服务、使用何种数据库,不属于本仓库能够独立确认的内容。
|
||||
开发环境中,浏览器可以通过 Vite 代理访问后端;生产环境则由部署网关转发接口,或通过构建变量 `VITE_REACT_APP_SERVER_URL` 连接独立后端域名;未设置或为空时使用同源地址。后端内部如何划分服务、使用何种数据库,不属于本仓库能够独立确认的内容。
|
||||
|
||||
### 3.2 启动链路
|
||||
|
||||
@@ -178,7 +178,7 @@ flowchart TD
|
||||
|
||||
| 顺序 | 文件 | 负责什么 |
|
||||
| --- | --- | --- |
|
||||
| 1 | `index.html` | 提供 `root` 节点、启动占位画面;先加载 `/config.js`,再加载入口模块 |
|
||||
| 1 | `index.html` | 提供 `root` 节点、启动占位画面;直接加载入口模块,不再加载 `/config.js` |
|
||||
| 2 | `src/index.jsx` | 注入缓存主题,安装资源加载失败恢复处理,初始化语言并挂载 React |
|
||||
| 3 | `src/context/Status/`、`User/`、`Theme/` | 提供站点配置、登录状态及主题能力 |
|
||||
| 4 | `src/components/layout/PageLayout.jsx` | 组织全局布局,加载 `/api/status`,按缓存状态决定是否校验 `/api/user/self` |
|
||||
@@ -196,7 +196,7 @@ flowchart TD
|
||||
```text
|
||||
monkey-ai-web/
|
||||
├── docs/ 本项目分析等维护文档,不参与前端页面模块加载
|
||||
├── public/ 静态资源和运行时 config.js
|
||||
├── public/ 原样复制到构建目录的静态资源
|
||||
├── scripts/ 国际化提取、翻译整理等辅助脚本
|
||||
├── tests/ 部分 Node 测试
|
||||
├── src/
|
||||
@@ -423,13 +423,7 @@ Copy-Item .env.example .env.local
|
||||
|
||||
### 7.3 推荐联调方式:浏览器同源请求
|
||||
|
||||
保持 `public/config.js` 中的 `SERVER_URL` 为空,且不设置 `VITE_REACT_APP_SERVER_URL`:
|
||||
|
||||
```js
|
||||
window.__APP_CONFIG__ = {
|
||||
SERVER_URL: '',
|
||||
};
|
||||
```
|
||||
不设置 `VITE_REACT_APP_SERVER_URL`(或设为空字符串),接口默认使用同源地址。
|
||||
|
||||
此时前端向当前域名发请求,由 Vite 将以下路径转发到 `vite.config.js` 中 `DEV_PROXY_TARGET` 指定的后端(当前为远程地址 `http://134.175.227.134:13000`,不是本机 3000 端口):
|
||||
|
||||
@@ -443,7 +437,7 @@ window.__APP_CONFIG__ = {
|
||||
|
||||
### 7.4 可选联调方式:直接连接独立后端
|
||||
|
||||
可以在 `public/config.js` 中将 `SERVER_URL` 设为后端根地址,或者在 `.env.local` 中配置:
|
||||
在 `.env.local` 中配置后端根地址(构建变量):
|
||||
|
||||
```dotenv
|
||||
VITE_REACT_APP_SERVER_URL=https://api.example.com
|
||||
@@ -452,15 +446,14 @@ VITE_CLOUD_BRAIN_UI_ENABLED=false
|
||||
|
||||
接口根地址优先级如下:
|
||||
|
||||
1. 非空的 `window.__APP_CONFIG__.SERVER_URL`。
|
||||
2. 非空的 `import.meta.env.VITE_REACT_APP_SERVER_URL`。
|
||||
3. 空字符串,使用当前页面域名。
|
||||
1. 非空的 `import.meta.env.VITE_REACT_APP_SERVER_URL`(构建变量)。
|
||||
2. 空字符串,使用当前页面域名(同源)。
|
||||
|
||||
地址填写后端根地址,不额外加 `/api`;具体请求自己包含路径。运行时地址为空时会继续回退至构建变量,不会强制覆盖为同源模式。
|
||||
地址填写后端根地址,不额外加 `/api`;具体请求自己包含路径。要使用同源模式,需确保构建变量未设置或为空。
|
||||
|
||||
直连独立域名时,请求不经过本地 Vite 代理,后端需要支持正确的 CORS、Cookie 和凭证策略。公共 Axios、对话 Fetch 与 SSE 都有各自的凭证配置,修改请求方式时需要分别核对。
|
||||
|
||||
`public/config.js` 会复制为 `dist/config.js`,部署后可修改并刷新页面;Vite 环境变量在构建时处理,开发时修改应重启服务,生产时修改应重新构建。前端配置对浏览器可见,只能存公开配置。
|
||||
已移除 `public/config.js` 及其入口脚本,不再支持部署后直接修改 `dist/config.js` 切换后端。旧部署如依赖它,需迁移到构建前设置 `VITE_REACT_APP_SERVER_URL`,或清空该变量后重新构建并配置同源反向代理。Vite 环境变量在构建时处理,开发时修改应重启服务,生产时修改应重新构建并发布;仅修改部署服务器环境变量或刷新页面不会改变已有产物。前端配置对浏览器可见,只能存公开配置。
|
||||
|
||||
### 7.5 按这个顺序确认环境可用
|
||||
|
||||
@@ -550,7 +543,7 @@ npm run preview
|
||||
| 正确区分页面与接口 | `/api` 等接口不能误回退到 HTML;带哈希的资源请求也应返回正确资源类型 |
|
||||
| 流式响应 | SSE 接口需要合理的超时与关闭响应缓冲,避免生成完毕才一次性显示 |
|
||||
| WebSocket | 使用时需配置连接升级转发;当前开发代理在 `/v1` 开启 `ws` |
|
||||
| 缓存策略 | `index.html`、`config.js` 应及时更新,带哈希资源可以长缓存 |
|
||||
| 缓存策略 | `index.html` 应及时更新,带哈希资源可以长缓存;后端地址变化需重建并发布配套 HTML 与资源,不再有独立 `config.js` 缓存 |
|
||||
| 部署根路径 | 当前资源和路由按域名根路径使用,子目录部署需检查 Vite `base`、路由及资源路径 |
|
||||
|
||||
`vercel.json` 当前仅配置 GitHub 通知静默,没有提供完整的路由回退和后端代理。不能把它当成已经验收过的部署方案。
|
||||
@@ -594,7 +587,7 @@ Node 测试和生产构建通过不等于后端业务或线上发布验收通过
|
||||
| 账号有权限,菜单却不显示 | 依次检查站点模块配置、企业功能、细分权限及构建开关 |
|
||||
| 菜单显示,点击后却跳回企业空间 | 检查 `org_only_mode`、`App.jsx` 的允许路径和企业页面守卫 |
|
||||
| 企业列表为空或数据范围不符 | 检查 `current_org_id`、显式 `orgId`、`X-Org-Id`、成员权限及业务响应 |
|
||||
| 修改环境变量未生效 | 检查运行时地址优先级、是否重启或重建、`config.js` 和浏览器缓存 |
|
||||
| 修改环境变量未生效 | 检查构建时的变量值、是否重启或重建并发布、HTML 与静态资源缓存 |
|
||||
| 流式回复在结束时一次性出现 | 检查 SSE 事件、网关缓冲、超时和后端输出方式 |
|
||||
| 线上刷新子页面 404 | 检查静态服务器的 SPA 路由回退 |
|
||||
| 模块资源加载失败 | 检查旧 HTML 与新资源是否版本不一致,确认发布和缓存策略 |
|
||||
@@ -622,7 +615,7 @@ Node 测试和生产构建通过不等于后端业务或线上发布验收通过
|
||||
| 构建、代理与路径别名 | [vite.config.js](../vite.config.js)、[jsconfig.json](../jsconfig.json) |
|
||||
| React 启动与布局 | [index.jsx](../src/index.jsx)、[PageLayout.jsx](../src/components/layout/PageLayout.jsx) |
|
||||
| 顶层路由与权限 | [App.jsx](../src/App.jsx)、[auth.jsx](../src/helpers/auth.jsx) |
|
||||
| 共享请求与配置 | [api.js](../src/helpers/api.js)、[config.js](../src/helpers/config.js)、[运行时配置](../public/config.js) |
|
||||
| 共享请求与配置 | [api.js](../src/helpers/api.js)、[config.js](../src/helpers/config.js)、[环境变量示例](../.env.example) |
|
||||
| 请求去重 | [requestDedupe.js](../src/helpers/requestDedupe.js)、[相关测试](../src/helpers/requestDedupe.test.js) |
|
||||
| 登录状态 | [User Provider](../src/context/User/index.jsx)、[User Reducer](../src/context/User/reducer.js) |
|
||||
| 企业入口与成员逻辑 | [Organization](../src/pages/Organization/index.jsx)、[useOrgMembers](../src/hooks/org/useOrgMembers.js) |
|
||||
|
||||
@@ -356,7 +356,6 @@
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<script src="/config.js"></script>
|
||||
<script type="module" src="/src/index.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
// Runtime configuration - modify per deployment environment.
|
||||
// Monolith mode: leave SERVER_URL empty.
|
||||
// Separated mode: set to backend URL, e.g. 'https://api.example.com'
|
||||
window.__APP_CONFIG__ = {
|
||||
SERVER_URL: '',
|
||||
};
|
||||
@@ -18,9 +18,5 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
export function getServerURL() {
|
||||
return (
|
||||
window.__APP_CONFIG__?.SERVER_URL ||
|
||||
import.meta.env.VITE_REACT_APP_SERVER_URL ||
|
||||
''
|
||||
);
|
||||
return import.meta.env.VITE_REACT_APP_SERVER_URL || '';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import test from 'node:test';
|
||||
import { transformWithEsbuild } from 'vite';
|
||||
|
||||
const source = await readFile(new URL('./config.js', import.meta.url), 'utf8');
|
||||
|
||||
async function loadConfig(env) {
|
||||
const { code } = await transformWithEsbuild(source, 'config.js', {
|
||||
loader: 'js',
|
||||
define: { 'import.meta.env': JSON.stringify(env) },
|
||||
});
|
||||
return import(
|
||||
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
|
||||
);
|
||||
}
|
||||
|
||||
for (const [name, env, expected] of [
|
||||
['missing backend URL uses same origin', {}, ''],
|
||||
['empty backend URL uses same origin', { VITE_REACT_APP_SERVER_URL: '' }, ''],
|
||||
[
|
||||
'configured backend URL works without a window global',
|
||||
{ VITE_REACT_APP_SERVER_URL: 'https://api.example.com' },
|
||||
'https://api.example.com',
|
||||
],
|
||||
]) {
|
||||
test(name, async () => {
|
||||
const { getServerURL } = await loadConfig(env);
|
||||
assert.equal(getServerURL(), expected);
|
||||
});
|
||||
}
|
||||
|
||||
test('HTML no longer loads the removed runtime configuration script', async () => {
|
||||
const html = await readFile(
|
||||
new URL('../../index.html', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.doesNotMatch(html, /<script\b[^>]*\bsrc=["']\/config\.js["']/i);
|
||||
});
|
||||
Reference in New Issue
Block a user