fix: SPA 路由回退的 HTML 补 no-cache 头(配合 #1732 白屏修复) #1735
Reference in New Issue
Block a user
Delete Branch "fix/nginx-html-nocache"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
背景
配合 #1732(懒加载 chunk 失效白屏 ErrorBoundary 自动刷新)。排查发现 nginx 缓存策略有两层缺陷,导致 index.html 实际被浏览器长期缓存:
location = /index.html只对直接访问/index.html生效;用户从/login、/app/dashboard等路由进入时走location /的try_files $uri /index.html回退,返回的 HTML 没有 no-cache 头(实测 staging 确认 /login、/index.html 均无 Cache-Control)。旧标签页因此长期缓存含旧 chunk 引用的 HTML,发版后点菜单 → 旧 chunk 404 → 白屏。修复
在
location /块加add_header Cache-Control "no-cache" always;:~* \.(js|css...)匹配,保持expires 1y + public, immutable,不受影响always保证 4xx/5xx 也带头5 份配置同步修改(infra/docker/nginx{,-staging,-production}.conf + deploy/configs/nginx{,-staging,-production}.conf),消除多份配置不一致的隐患。
部署说明
服务器 bind mount 的配置是独立文件(不在镜像里),合并后需把最新 nginx-staging.conf 同步到
/var/lib/xiaoxia-saas-staging/nginx-staging.conf并nginx -s reload(staging 生效);生产发布时同步 nginx-production.conf。此步由部署流程/前端协助在服务器执行。验证
部署后实测:
/login、/app/assets等路由的 HTML 响应头含cache-control: no-cachecache-control: public, immutable(max-age=31536000)问题排查发现两层缺陷: 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(保持一致,避免再混用)🚀 预览环境已部署
代码审查结果 - PR #1735
⚠️ 问题(1个需要修改)
add_header指令会导致该 location 块不再继承server层级的 header。如果server块中配置了安全头(如X-Frame-Options,Strict-Transport-Security,X-Content-Type-Options等),在location /的响应中这些头将会丢失,从而引发安全风险或功能异常。必须检查并在此处补全必要的安全头。💡 建议(1个可选)
location /,静态资源将被错误地设置为no-cache,导致性能下降。✅ 格式检查通过 | ❌ 逻辑审查需修改 | ✅ 性能无明显问题
🤖 由 AI 代码审查机器人自动生成 | 2026-09-06 05:57:34 | 模型:
🗑️ 预览环境已清理
PR #1735 已关闭或合并,对应的预览环境已被清理。