From a1ab1ea4b3c2eafc5e562e3f4a01cc17782963cb Mon Sep 17 00:00:00 2001 From: Nixevol Date: Thu, 9 Jul 2026 17:16:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20vite=20dev=20=E8=87=AA=E5=8A=A8=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E5=88=B0=E5=B8=A6=E5=B0=BE=E6=96=9C=E6=9D=A0=E7=9A=84?= =?UTF-8?q?=20base=20=E5=B9=B6=E9=9D=99=E9=BB=98=E5=A4=84=E7=90=86=20API?= =?UTF-8?q?=20=E9=87=8D=E5=90=AF=E6=9C=9F=E4=BB=A3=E7=90=86=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - admin/client 新增 redirectToBase 插件,访问 /admin 或 / 自动 302 到 /admin/(client 同理 /trace/) - admin /api 代理增加错误处理,API 重启空档返回 503 让前端静默重试,避免 ECONNREFUSED 堆栈刷屏 --- aidocs/project_context.md | 1 + apps/admin/vite.config.ts | 57 ++++++++++++++++++++++++++++++-------- apps/client/vite.config.ts | 30 ++++++++++++++++++-- 3 files changed, 74 insertions(+), 14 deletions(-) diff --git a/aidocs/project_context.md b/aidocs/project_context.md index 5ccc1b7..bad8f1a 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -273,6 +273,7 @@ - 安装页 `baseUrlHint`(中英):三行换行说明对外访问地址(域名 / IP:端口 / 本地默认);`.hint` 使用 `white-space: pre-line` 渲染 `\n`。 - 安装页错误信息穿透(2026-07-09):`InstallService.testConnection/testStorage/install` 内部原先 `throw new Error(...)`,会被全局 `AllExceptionsFilter` 当作未知异常统一替换为「服务器内部错误」……已用直连接口验证。 - 安装提交 JSON 解析失败(2026-07-09):根因是安装成功后 `process.exit(0)`(600ms)在本地 dev 环境掐断 HTTP 连接,前端 `res.json()` 收到空 body 报 `Unexpected end of JSON input`。修复:① `ARTVERIS_INSTALL_MODE=1` 本地安装调试不再 `process.exit`,保证 JSON 响应完整返回;生产环境延迟 2.5s 再退出供 PM2/Docker 重启。② `runPrismaMigrate` 失败改为 `BusinessException` 透传「数据库迁移失败:…」。③ `status()` 新增 `needsDevRestart`(本地 .env 已写但进程仍为 InstallAppModule)。④ `Install.vue` 用 `parseApiBody` 安全解析空/非法 JSON。 +- vite dev 访问体验(2026-07-09):① admin/client 的 base 分别为 `/admin/`、`/trace/`,dev 下直接访问 `/admin`(无尾斜杠)或根 `/` 时 vite 只提示 "did you mean /admin/" 不跳转——两个 `vite.config.ts` 各加 `redirectToBase` 插件(`configureServer` 中间件),把 `/` 与不带尾斜杠的 base 302 到带尾斜杠的 base(保留 query)。② admin 代理 `/api` 增加 `configure` 错误处理 `onProxyError`:安装完成自动切换、API 重启的空档里前端仍在轮询 `/api/install/status`,连接被拒时返回 503 让前端静默重试,避免大段 `ECONNREFUSED` 堆栈。(生产单端口由 API `mountSpa` 托管 `/admin`→index,无此问题。) - 本地 dev 进程管理:`--stop` + 安装后自动切换(2026-07-09,明确不引入 PM2):本地 dev 仍是「前台阻塞 + Job Object/控制台事件整树清理」模型,PM2 仅用于生产(build.py 的 `ecosystem.config.cjs`)。新增两点 PM2 式体验:① `python scripts/dev.py --stop`:复用 `kill_project_servers(ports)` 一键停掉 api/admin/client 后退出,零新依赖。② 安装完成自动切正式模式:无 `.env` 进入安装向导后,`dev.py` 用 `wait_for_install_env(ENV_FILE, api_proc)` 持续监听根 `.env` 出现——因后端在迁移+建超管全部成功后**最后一步**才写 `.env`,失败尝试与填写过程都不写,故「文件出现且非空」是安全的完成信号,**天然避开安装中重启**;一旦检测到即 `stop_proc_tree` 安装 API + `free_port(3000)`,保留 admin vite 不断连,以正式模式重启 `dev:api`+`dev:client`(不再迁移/种子,避免覆盖向导设置的管理员),随后 `p.wait()` 阻塞,Ctrl+C 仍整树清理。`--install-mode` 调试模式写临时 `.env.install-dev`、不自动切换(保留手动重run)。前端 `submit()` 相应简化为:成功后统一 `pollUntilReady()`(等 `installed && !needsDevRestart`)就绪跳后台,超时兜底提示 `doneLocalDev`。`dev.py` 导入补 `free_port`/`stop_proc_tree`。验证:api/admin build 通过、`py_compile` 通过。 ### apps/client(Vue3+Vite 移动优先溯源页) diff --git a/apps/admin/vite.config.ts b/apps/admin/vite.config.ts index df6b028..05b8346 100644 --- a/apps/admin/vite.config.ts +++ b/apps/admin/vite.config.ts @@ -1,10 +1,51 @@ -import { defineConfig } from 'vite'; +import { defineConfig, type Plugin } from 'vite'; import vue from '@vitejs/plugin-vue'; import { fileURLToPath, URL } from 'node:url'; +import type { ServerResponse } from 'node:http'; + +const BASE = process.env.VITE_BASE || '/admin/'; + +// dev 下访问根路径或不带尾斜杠的 base(如 /admin)时自动 302 到 /admin/, +// 避免 vite 只提示 "did you mean /admin/" 而不跳转。 +function redirectToBase(base: string): Plugin { + const baseNoSlash = base.replace(/\/$/, ''); + return { + name: 'redirect-to-base', + configureServer(server) { + server.middlewares.use((req, res, next) => { + const url = req.url || '/'; + const path = url.split('?')[0]; + if (path === '/' || path === baseNoSlash) { + const query = url.slice(path.length); + res.statusCode = 302; + res.setHeader('Location', base + query); + res.end(); + return; + } + next(); + }); + }, + }; +} + +// API 重启期间(如安装完成自动切换)代理连接被拒属正常,返回 503 让前端静默重试, +// 避免抛出大段 ECONNREFUSED 堆栈。 +function onProxyError(proxy: { on: (e: string, cb: (...a: unknown[]) => void) => void }): void { + proxy.on('error', (...args: unknown[]) => { + const res = args[2] as (ServerResponse & { headersSent?: boolean }) | undefined; + if (!res) return; + if (!res.headersSent && typeof res.writeHead === 'function') { + res.writeHead(503, { 'Content-Type': 'application/json' }); + } + if (typeof res.end === 'function') { + res.end('{"code":1005,"message":"service restarting","data":null}'); + } + }); +} export default defineConfig({ - base: process.env.VITE_BASE || '/admin/', - plugins: [vue()], + base: BASE, + plugins: [vue(), redirectToBase(BASE)], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), @@ -15,14 +56,8 @@ export default defineConfig({ port: 5173, host: true, proxy: { - '/api': { - target: 'http://localhost:3000', - changeOrigin: true, - }, - '/uploads': { - target: 'http://localhost:3000', - changeOrigin: true, - }, + '/api': { target: 'http://localhost:3000', changeOrigin: true, configure: onProxyError }, + '/uploads': { target: 'http://localhost:3000', changeOrigin: true }, }, }, }); diff --git a/apps/client/vite.config.ts b/apps/client/vite.config.ts index d5109a5..dc6f854 100644 --- a/apps/client/vite.config.ts +++ b/apps/client/vite.config.ts @@ -1,10 +1,34 @@ -import { defineConfig } from 'vite'; +import { defineConfig, type Plugin } from 'vite'; import vue from '@vitejs/plugin-vue'; import { fileURLToPath, URL } from 'node:url'; +const BASE = process.env.VITE_BASE || '/trace/'; + +// dev 下访问根路径或不带尾斜杠的 base(如 /trace)时自动 302 到 /trace/。 +function redirectToBase(base: string): Plugin { + const baseNoSlash = base.replace(/\/$/, ''); + return { + name: 'redirect-to-base', + configureServer(server) { + server.middlewares.use((req, res, next) => { + const url = req.url || '/'; + const path = url.split('?')[0]; + if (path === '/' || path === baseNoSlash) { + const query = url.slice(path.length); + res.statusCode = 302; + res.setHeader('Location', base + query); + res.end(); + return; + } + next(); + }); + }, + }; +} + export default defineConfig({ - base: process.env.VITE_BASE || '/trace/', - plugins: [vue()], + base: BASE, + plugins: [vue(), redirectToBase(BASE)], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)),