fix: 静态资源 Cache-Control 防止浏览器缓存旧 index.html 导致白屏

- assets/* 设为 immutable 长期缓存(文件名含 hash,内容变更即换名)
- index.html 和 SPA fallback 设为 no-cache(每次重新验证)
- 彻底解决 MIME type 错误白屏问题
This commit is contained in:
opsx-tools committed 2026-07-23 12:59:11 +08:00
1 parent 91974fc30f
commit 7de1f70905
1 file changed
+8 -1
+8 -1
View File
@@ -78,10 +78,17 @@ func main() {
}
if f, err := dist.Open(path); err == nil {
f.Close()
// 带 hash 的静态资源(assets/*)可长期缓存;index.html 必须每次重新验证
if strings.HasPrefix(path, "assets/") {
c.Header("Cache-Control", "public, max-age=31536000, immutable")
} else {
c.Header("Cache-Control", "no-cache")
}
fileServer.ServeHTTP(c.Writer, c.Request)
return
}
// SPA fallback
// SPA fallback — 必须 no-cache,否则浏览器缓存旧 index.html 会白屏
c.Header("Cache-Control", "no-cache")
c.Request.URL.Path = "/"
fileServer.ServeHTTP(c.Writer, c.Request)
})