服务: SPA 缓存策略修复重建后首页黑屏
- index.html 设 Cache-Control: no-cache,避免浏览器启发式缓存保留引用已删除 chunk 的旧 HTML - /assets/*(文件名含内容 hash,不可变)设 immutable 长缓存,减少回源 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -34,7 +34,13 @@ func NewRouter(a *app.App, gw *proxy.Gateway) *gin.Engine {
|
|||||||
// 静态资源(前端构建产物,存在时托管)
|
// 静态资源(前端构建产物,存在时托管)
|
||||||
const dist = "web/dist"
|
const dist = "web/dist"
|
||||||
if _, err := os.Stat(dist); err == nil {
|
if _, err := os.Stat(dist); err == nil {
|
||||||
r.Static("/assets", dist+"/assets")
|
// /assets 文件名含内容 hash,不可变:长缓存 + immutable,避免每次回源
|
||||||
|
assets := r.Group("/assets")
|
||||||
|
assets.Use(func(c *gin.Context) {
|
||||||
|
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
||||||
|
c.Next()
|
||||||
|
})
|
||||||
|
assets.Static("", dist+"/assets")
|
||||||
r.StaticFile("/favicon.svg", dist+"/favicon.svg")
|
r.StaticFile("/favicon.svg", dist+"/favicon.svg")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,6 +54,8 @@ func NewRouter(a *app.App, gw *proxy.Gateway) *gin.Engine {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(dist); err == nil {
|
if _, err := os.Stat(dist); err == nil {
|
||||||
|
// SPA 入口:必须每次回源校验,否则浏览器启发式缓存会保留引用已删除 chunk 的旧 HTML → 黑屏
|
||||||
|
c.Header("Cache-Control", "no-cache")
|
||||||
c.File(dist + "/index.html")
|
c.File(dist + "/index.html")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user