From f066a8ef4efa40159fdb01feecf94f7daa3cf4f5 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Sun, 6 Sep 2026 13:55:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SPA=20=E8=B7=AF=E7=94=B1=E5=9B=9E?= =?UTF-8?q?=E9=80=80=E7=9A=84=20HTML=20=E8=A1=A5=20no-cache=20=E5=A4=B4?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=8F=91=E7=89=88=E5=90=8E=E6=97=A7=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E9=A1=B5=E7=99=BD=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题排查发现两层缺陷: 1. 运行时 bind mount 的 nginx 配置是服务器旧版(无 index.html no-cache 块); 2. 即使最新仓库配置,location = /index.html 只对直接访问 /index.html 生效, 用户从 /login、/app/dashboard 等路由进入时走 location / 的 try_files 回退, 返回的 HTML 没有 no-cache 头——旧标签页会长期缓存含旧 chunk 引用的 HTML, 发版后点菜单就 Failed to fetch dynamically imported module 白屏。 修复:在 location / 块直接 add_header Cache-Control no-cache: - try_files 回退的 SPA HTML 与 HTML 文档每次校验,发版后能拿到新 chunk 引用 - 带 hash 的静态资源由更高优先级的 location ~* \.(js|css...) 匹配, 保持 expires 1y + immutable,不受影响 5 份配置同步:infra/docker/nginx{,-staging,-production}.conf + deploy/configs/nginx{,-staging,-production}.conf(保持一致,避免再混用) --- deploy/configs/nginx-production.conf | 4 ++++ deploy/configs/nginx-staging.conf | 4 ++++ infra/docker/nginx-production.conf | 4 ++++ infra/docker/nginx-staging.conf | 4 ++++ infra/docker/nginx.conf | 4 ++++ 5 files changed, 20 insertions(+) diff --git a/deploy/configs/nginx-production.conf b/deploy/configs/nginx-production.conf index 70b4b1a02..1944f8f1f 100644 --- a/deploy/configs/nginx-production.conf +++ b/deploy/configs/nginx-production.conf @@ -14,6 +14,10 @@ server { # SPA routing - index.html 禁止缓存,确保每次获取最新版本 location / { try_files $uri /index.html; + # HTML 文档(含 try_files 回退的 SPA 路由,如 /login /app/dashboard)一律 no-cache, + # 每次校验 ETag/Last-Modified,保证发版后旧标签页重新加载拿到新 chunk 引用; + # 带 hash 的静态资源由下方 ~* \.(js|css...) location 优先匹配,不受影响、保持 immutable + add_header Cache-Control "no-cache" always; } # API proxy — Production 环境代理到 production API 容器 diff --git a/deploy/configs/nginx-staging.conf b/deploy/configs/nginx-staging.conf index cc6cc4ab9..9521dbb42 100644 --- a/deploy/configs/nginx-staging.conf +++ b/deploy/configs/nginx-staging.conf @@ -21,6 +21,10 @@ server { # SPA fallback location / { try_files $uri /index.html; + # HTML 文档(含 try_files 回退的 SPA 路由,如 /login /app/dashboard)一律 no-cache, + # 每次校验 ETag/Last-Modified,保证发版后旧标签页重新加载拿到新 chunk 引用; + # 带 hash 的静态资源由下方 ~* \.(js|css...) location 优先匹配,不受影响、保持 immutable + add_header Cache-Control "no-cache" always; } # API proxy — Staging 环境代理到 staging API 容器 diff --git a/infra/docker/nginx-production.conf b/infra/docker/nginx-production.conf index c80cfa7b5..c5269f003 100755 --- a/infra/docker/nginx-production.conf +++ b/infra/docker/nginx-production.conf @@ -16,6 +16,10 @@ server { # 注意:不能加 $uri/,否则 /assets 等与构建产物目录同名的路由会被当成目录访问,返回 403 location / { try_files $uri /index.html; + # HTML 文档(含 try_files 回退的 SPA 路由,如 /login /app/dashboard)一律 no-cache, + # 每次校验 ETag/Last-Modified,保证发版后旧标签页重新加载拿到新 chunk 引用; + # 带 hash 的静态资源由下方 ~* \.(js|css...) location 优先匹配,不受影响、保持 immutable + add_header Cache-Control "no-cache" always; } # API proxy diff --git a/infra/docker/nginx-staging.conf b/infra/docker/nginx-staging.conf index d92cdb789..f6ec40cda 100755 --- a/infra/docker/nginx-staging.conf +++ b/infra/docker/nginx-staging.conf @@ -23,6 +23,10 @@ server { location / { try_files $uri /index.html; + # HTML 文档(含 try_files 回退的 SPA 路由,如 /login /app/dashboard)一律 no-cache, + # 每次校验 ETag/Last-Modified,保证发版后旧标签页重新加载拿到新 chunk 引用; + # 带 hash 的静态资源由下方 ~* \.(js|css...) location 优先匹配,不受影响、保持 immutable + add_header Cache-Control "no-cache" always; } # API proxy diff --git a/infra/docker/nginx.conf b/infra/docker/nginx.conf index a5b581f7b..bd8a1fe68 100755 --- a/infra/docker/nginx.conf +++ b/infra/docker/nginx.conf @@ -33,6 +33,10 @@ server { location / { try_files $uri /index.html; + # HTML 文档(含 try_files 回退的 SPA 路由,如 /login /app/dashboard)一律 no-cache, + # 每次校验 ETag/Last-Modified,保证发版后旧标签页重新加载拿到新 chunk 引用; + # 带 hash 的静态资源由下方 ~* \.(js|css...) location 优先匹配,不受影响、保持 immutable + add_header Cache-Control "no-cache" always; } # API proxy -- 2.54.0