fix: nginx index.html 禁用浏览器缓存,防止部署后旧 chunk 引用导致白屏 #1212
Reference in New Issue
Block a user
Delete Branch "fix/nginx-index-no-cache"
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?
问题
当前 nginx 配置的
location /对 index.html 没有设置任何 Cache-Control 头。浏览器会按默认策略缓存 HTML 文件。Vite 构建的 JS/CSS 文件名自带 content hash,配置了
expires 1y长期缓存——这是正确的。但问题在于:每次部署后 chunk 文件名变化(新 hash),但浏览器仍用缓存的旧 index.html 指向旧 chunk,导致
Failed to fetch dynamically imported module白屏。修复方案
在
location /块上方新增精确匹配块:影响范围
infra/docker/nginx.conf(production)infra/docker/nginx-staging.conf(staging)两个文件都做了相同的修改。
验证方式
部署后,浏览器 DevTools → Network → 查看 index.html 响应头:
Cache-Control: no-cache, no-store, must-revalidatePragma: no-cache注意事项
location = /index.html是精确匹配,优先级高于location /(前缀匹配)【阻塞级判定】
📊 审查概览
🔴 阻塞级问题(必须修复)
无
💡 改进建议(不阻塞合并)
无
✅ 良好实践
location = /index.html精确匹配优先级高于前缀匹配,确保缓存控制策略仅针对根目录入口文件生效,避免误拦截其他静态资源。no-cache,no-store,must-revalidate以及Pragma和Expires,能够最大程度兼容各种浏览器和代理服务器,确保 SPA 部署后用户始终获取最新的 HTML 文件,有效避免“白屏”或旧版本 Chunk 加载错误。🤖 由 AI 代码审查机器人自动生成 | 2026-08-01 10:21:25 | 模型:
🚀 预览环境已部署
🗑️ 预览环境已清理
PR #1212 已关闭或合并,对应的预览环境已被清理。