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) })