From 7de1f70905ded1dd24e2470a77d37ffb4c84a234 Mon Sep 17 00:00:00 2001 From: opsx-tools Date: Thu, 23 Jul 2026 12:59:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=9D=99=E6=80=81=E8=B5=84=E6=BA=90=20C?= =?UTF-8?q?ache-Control=20=E9=98=B2=E6=AD=A2=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=97=A7=20index.html=20=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=99=BD=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - assets/* 设为 immutable 长期缓存(文件名含 hash,内容变更即换名) - index.html 和 SPA fallback 设为 no-cache(每次重新验证) - 彻底解决 MIME type 错误白屏问题 --- backend/main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/main.go b/backend/main.go index 3d8a34a..1cfce1b 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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) })