Skip to content

是的,index.html 会被浏览器缓存,但具体行为取决于服务端的缓存策略配置。以下是详细分析和解决方案:


1. 为什么 index.html 会被缓存?

  • 默认行为:浏览器会根据服务端返回的 Cache-ControlExpires 等 HTTP 头部决定是否缓存 index.html
  • 常见场景
    • 未配置缓存策略时,浏览器可能默认缓存 index.html(尤其是强缓存)。
    • 静态服务器(如 Nginx/Apache)可能对 HTML 文件设置缓存。

2. 如何防止 index.html 缓存?

方案 1:服务端配置 HTTP 头部(推荐)

通过服务端强制 index.html 不缓存或短时间缓存:

http
Cache-Control: no-store, no-cache, must-revalidate
Expires: 0
Pragma: no-cache

Nginx 配置示例

nginx
location = /index.html {
  add_header Cache-Control "no-cache, no-store, must-revalidate";
  add_header Pragma "no-cache";
  add_header Expires "0";
}

方案 2:添加版本号/哈希参数

在引用 index.html 的 URL 中动态添加参数(如时间戳或版本号):

html
<link rel="stylesheet" href="styles.css?v=1.0.1">
<script src="app.js?t=20230703"></script>

单页应用(SPA)示例

js
// 前端代码中动态加载资源
const version = new Date().getTime();
fetch(`/index.html?nocache=${version}`);

方案 3:使用 Service Worker 控制缓存

sw.js 中跳过 index.html 的缓存:

js
self.addEventListener('fetch', (event) => {
  if (event.request.url.includes('index.html')) {
    event.respondWith(
      fetch(event.request).then(response => {
        return response;
      }).catch(() => caches.match('/fallback.html'))
    );
  }
});

3. 验证 index.html 是否被缓存

  • 浏览器开发者工具
    1. 打开 Network 面板。
    2. 刷新页面,查看 index.html 的请求状态:
      • 如果状态为 200 (from disk cache),说明被强缓存。
      • 如果状态为 304 (Not Modified),说明被协商缓存。
  • 命令行工具
    bash
    curl -I http://yourdomain.com/index.html
    检查返回的 Cache-ControlExpires 头部。

4. 特殊情况处理

CDN 缓存

如果使用 CDN(如 Cloudflare、AWS CloudFront),需额外配置:

  • 设置 index.html 的缓存时间为 0。
  • 触发 CDN 缓存刷新(Purge Cache)。

单页应用(SPA)路由问题

  • 问题:用户直接访问子路由(如 /dashboard)时,可能因缓存的 index.html 版本过旧导致路由失败。
  • 解决:确保服务器始终返回最新的 index.html(配置 Nginx/Apache 的 try_files 或 Fallback)。

最佳实践

  1. 生产环境
    • index.html 设置 no-cache 或短缓存(如 max-age=60)。
    • 对其他静态资源(JS/CSS/图片)使用 文件名哈希 + 长期缓存(如 max-age=31536000, immutable)。
  2. 开发环境
    • 禁用所有缓存(或使用 vite/webpack-dev-server 的热更新)。
  3. 紧急更新
    • 通过版本号或 CDN 缓存刷新强制更新。

总结

  • index.html 默认可能被缓存,需通过服务端配置或版本控制避免。
  • 优先使用 HTTP 头部控制 + 文件名哈希 的组合方案。
  • 对于 SPA/PWA,需结合 Service Worker 和服务器路由配置处理缓存问题。