From 57652ae581571f61188808b04dd922855c0372b1 Mon Sep 17 00:00:00 2001 From: sophie Date: Fri, 28 Aug 2026 11:34:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(build):=20vite=E9=83=A8=E7=BD=B2=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E9=83=A8=E7=BD=B2=E5=90=8E=E6=9C=89=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=8F=96=E5=88=B0=E6=97=A7=E7=9A=84chunk=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 2 +- ...rprise_AI_Gateway_Architecture_20260222.md | 13 ++++ docs/project_context.md | 26 ++++---- frontend/nginx.conf | 13 ++++ frontend/src/app/router/guards.ts | 22 +++++++ scripts/nginx.conf | 60 ------------------- scripts/supervisord.conf | 34 ----------- 7 files changed, 62 insertions(+), 108 deletions(-) delete mode 100644 scripts/nginx.conf delete mode 100644 scripts/supervisord.conf diff --git a/AGENTS.md b/AGENTS.md index a833bfbc..317f2733 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -14,7 +14,7 @@ | --- | --- | | 前端 | Vue 3 (Composition API) + TypeScript + Vite + TailwindCSS + Pinia + Vue Router + Vue I18n | | 后端 | Flask (Python 3.12) + Gevent + SQLAlchemy 2.0 + Flask-Migrate(Alembic) + Gunicorn | -| 数据/基建 | PostgreSQL + Redis + Nginx + Supervisor + Docker Compose | +| 数据/基建 | PostgreSQL + Redis + Nginx + Docker Compose | | 集成 | Dify、企业微信、OA/HR(SQLServer)、PLM(Oracle)、MinIO | | 研发协作 | Gitee 托管 + Jenkins CI/CD(dev/test/prod 多环境) | diff --git a/docs/architecture/k_Enterprise_AI_Gateway_Architecture_20260222.md b/docs/architecture/k_Enterprise_AI_Gateway_Architecture_20260222.md index 30a646c6..c858df27 100644 --- a/docs/architecture/k_Enterprise_AI_Gateway_Architecture_20260222.md +++ b/docs/architecture/k_Enterprise_AI_Gateway_Architecture_20260222.md @@ -55,6 +55,19 @@ location /api/ { } ``` +同时为静态与 `index.html` 设置了缓存策略,避免部署后浏览器/网关缓存旧版 `index.html`,引用部署时已删除的旧 hash 产物,导致登录后懒加载 chunk 404(报 `Failed to fetch dynamically imported module`): + +```nginx +# Vite 构建产物(带 hash、内容寻址):可长期 immutable 缓存,文件名一变即失效 +location /assets/ { + add_header Cache-Control "public, max-age=31536000, immutable"; +} +# index.html 必须始终重新校验,避免引用旧 hash +location = /index.html { + add_header Cache-Control "no-cache, no-store, must-revalidate"; +} +``` + #### 5.2.3 自动化部署脚本 (`scripts/deploy_compose.sh`) 当前标准部署入口(Jenkins 与服务器手工均使用此脚本): 1. **环境选择**: 参数 `dev` / `test` / `prod`,对应 `.env.dev`、`.env.test`、`.env.prod` 与 `.env.shared`。 diff --git a/docs/project_context.md b/docs/project_context.md index 2684d857..8cb00189 100644 --- a/docs/project_context.md +++ b/docs/project_context.md @@ -24,7 +24,7 @@ ### 基础设施 (Infrastructure) - **反向代理**: Nginx (处理 HTTPS, 静态资源, 反向代理) -- **进程管理**: Supervisor (守护 Gunicorn 进程) +- **进程管理**: Docker Compose 容器化(容器内由 CMD 启动 Gunicorn / Worker) - **数据库**: PostgreSQL (生产环境) - **缓存**: Redis (可选,用于 Session 或 任务队列) - **代码托管**: Gitee (`super` 分支) @@ -55,31 +55,31 @@ hxops/ │ └── vite.config.ts # Vite 配置 ├── scripts/ # 运维与自动化脚本 │ ├── deploy_compose.sh # Docker Compose 部署(Jenkins / 服务器) -│ ├── nginx.conf # Nginx 生产配置 -│ └── supervisord.conf # Supervisor 配置 └── docs/ # 项目文档 ``` ## 4. 🏗️ 部署架构 (Deployment Architecture) ### 4.1 生产环境架构 -为了满足高并发(500+ QPS)及稳定性要求,架构设计如下: +采用 Docker Compose 容器化部署,组件职责分离: -1. **Nginx (接入层)**: - - 监听 80/443 端口。 +1. **前置 Nginx / nginx-gateway (接入层)**: + - 对外生产入口(正式域名 `ai.well-trust.com:8881`),反向代理前端容器。 - **关键配置**: `proxy_buffering off;` (必须关闭缓冲以支持 SSE 流式响应)。 - - 提供前端静态资源 (`/dist`) 服务,开启 Gzip 压缩。 -2. **Supervisor (进程管理)**: - - 守护 `hxops-backend` 进程,负责自动重启和日志管理。 +2. **hxops-frontend (Nginx 容器)**: + - 多阶段构建:Node 生成 `dist`,Nginx 托管静态资源并反代 `/api/` 到后端容器。 + - 容器内监听 `80`,宿主机映射 `8890`;配置见 `frontend/nginx.conf`。 -3. **Gunicorn + Gevent (应用层)**: - - 运行 Flask 应用。 +3. **hxops-backend (应用层)**: + - Flask + Gunicorn + Gevent,容器内监听 `5000`(经前端容器 `hxops-backend:5000` 反代)。 - **Worker**: `gevent` (异步模式)。 - **并发**: `workers = CPU * 2 + 1`, `worker_connections = 1000+`。 - - 监听 `127.0.0.1:5052`。 -4. **PostgreSQL (数据层)**: +4. **后台 Worker**: + - `hxops-kb-worker` / `hxops-schedule-worker` / `hxops-shipping-worker`,与 backend 共用镜像,分担知识库、排程与发货通知任务。 + +5. **PostgreSQL (数据层)**: - 存储用户、应用配置及审计日志。 ### 4.2 CI/CD 工作流 diff --git a/frontend/nginx.conf b/frontend/nginx.conf index d5182a56..b0442e05 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -14,6 +14,19 @@ server { try_files $uri $uri/ /index.html; } + # Vite 构建产物(/assets/ 下带 hash 文件):内容寻址,文件名一变即换新缓存,可放心长期 immutable 缓存。 + location /assets/ { + root /usr/share/nginx/html; + add_header Cache-Control "public, max-age=31536000, immutable"; + } + + # index.html 必须始终重新校验(no-cache):避免浏览器/网关缓存旧版 HTML,引用部署后已删除的旧 hash 产物, + # 导致登录后懒加载 chunk 404,报 "Failed to fetch dynamically imported module"。 + location = /index.html { + root /usr/share/nginx/html; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } + # API 代理到后端 location /api/ { # 通过 Docker 内置 DNS 动态解析后端服务,避免后端容器重建后 diff --git a/frontend/src/app/router/guards.ts b/frontend/src/app/router/guards.ts index c1f50154..ba77f35c 100644 --- a/frontend/src/app/router/guards.ts +++ b/frontend/src/app/router/guards.ts @@ -34,6 +34,26 @@ function resolveAppBaseTitle(): string { return String(i18n.global.t(config.appTitleKey)); } +/** + * 懒加载 chunk 失败自愈。 + * 用途/触发时机:部署后若浏览器(或网关)缓存了旧版 index.html,会引用已删除的旧 hash 产物, + * 登录等页面跳转时动态 import() 该 chunk 失败 → 报 "Failed to fetch dynamically imported module" + * 并被登录流程误判为“登录失败”。此处检测到该类失败时做一次整页刷新(带 sessionStorage 防循环守卫), + * 刷新后取到最新 index.html 与 chunk,用户即可正常进入,不再看到误导性报错。 + * 核心逻辑:仅匹配 chunk 加载失败错误;同一标签页会话内只刷新一次,避免服务器持续异常时无限刷新。 + * @param router - Vue Router 实例 + */ +function setupChunkLoadRecovery(router: Router): void { + router.onError((error) => { + const message = String((error as Error)?.message || ''); + if (!message.includes('Failed to fetch dynamically imported module')) return; + const recoveryKey = 'hxops_chunk_load_recovery_once'; + if (sessionStorage.getItem(recoveryKey)) return; + sessionStorage.setItem(recoveryKey, '1'); + window.location.reload(); + }); +} + export function setupRouterGuards(router: Router): void { router.beforeEach((to, _from, next) => { const auth = useAuthStore(); @@ -69,4 +89,6 @@ export function setupRouterGuards(router: Router): void { const pageTitle = resolveRouteTitle(to.meta.title); document.title = pageTitle ? `${pageTitle} - ${baseTitle}` : baseTitle; }); + + setupChunkLoadRecovery(router); } diff --git a/scripts/nginx.conf b/scripts/nginx.conf deleted file mode 100644 index 5f1feda7..00000000 --- a/scripts/nginx.conf +++ /dev/null @@ -1,60 +0,0 @@ - -# hxops_nginx.conf -# 放置于 /etc/nginx/conf.d/hxops.conf - -server { - listen 80; - server_name 192.168.100.230; # 或域名 - - # 1. 前端静态资源 (动静分离) - location / { - root /opt/hxops/frontend/dist; - index index.html; - try_files $uri $uri/ /index.html; - - # 静态资源缓存策略 - location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { - expires 30d; - add_header Cache-Control "public, no-transform"; - } - } - - # 2. 后端 API 反向代理 - location /api/ { - # 转发到 Gunicorn (Gevent) - proxy_pass http://127.0.0.1:5000; - - # 请求体上限(主要约束上传;库存 ZIP 下载靠下方超时) - # 按业务估算:几十个大 Excel 打包约 100–300MB,预留 200m;若单批更大可改 500m - client_max_body_size 200m; - # 大文件响应超时(库存 ZIP 下载关键项) - proxy_send_timeout 300s; - send_timeout 300s; - - # 关键配置:关闭缓冲以支持 SSE 流式输出 - proxy_buffering off; - proxy_cache off; - - # 传递客户端真实 IP - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - - # 长连接配置 - proxy_http_version 1.1; - proxy_set_header Connection ""; - - # 超时设置 (应对长对话) - proxy_read_timeout 300s; - proxy_connect_timeout 75s; - } - - # 开启 Gzip 压缩 - gzip on; - gzip_min_length 1k; - gzip_comp_level 6; - gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; - gzip_vary on; - gzip_disable "MSIE [1-6]\."; -} diff --git a/scripts/supervisord.conf b/scripts/supervisord.conf deleted file mode 100644 index dc5c94ba..00000000 --- a/scripts/supervisord.conf +++ /dev/null @@ -1,34 +0,0 @@ - -; hxops_supervisord.conf -; 放置于 /etc/supervisor/conf.d/hxops.conf - -[program:hxops-backend] -; 启动命令:使用 Gunicorn 启动 Flask 应用 -command=/opt/hxops/backend/venv/bin/gunicorn -c /opt/hxops/backend/deploy/gunicorn_config.py "app:create_app('production')" - -; 工作目录 -directory=/opt/hxops/backend - -; 启动用户 -user=root - -; 自动启动 -autostart=true -autorestart=true - -; 启动重试次数 -startretries=3 - -; 停止信号 -stopsignal=QUIT - -; 日志配置 -stdout_logfile=/var/log/hxops/backend.log -stdout_logfile_maxbytes=50MB -stdout_logfile_backups=10 -stderr_logfile=/var/log/hxops/backend_err.log -stderr_logfile_maxbytes=50MB -stderr_logfile_backups=10 - -; 环境变量 -environment=FLASK_APP=run.py,FLASK_ENV=production,DATABASE_URL="postgresql://hxops:password@localhost/hxops_prod"