From fb9ae11aa8f24ee33aeb6ce7106b1eec64f396b2 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 7 May 2026 12:05:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=BA=E5=89=8D=E7=AB=AF=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B5=84=E6=BA=90=E8=AE=BE=E7=BD=AE=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=20Cache-Control=20=E5=93=8D=E5=BA=94=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复升级前端版本后,用户必须退出重新登录才能看到新版页面的问题。 根因:nginx 和 Lite 版 Go 静态服务均未设置 Cache-Control,浏览器按 启发式策略长期缓存 index.html,导致刷新仍加载旧版入口 HTML,旧版 引用的 hash 文件名也就不会变更。 改动: - frontend/nginx.conf: index.html 及 SPA fallback 设 no-cache, must-revalidate;/assets/* (Vite 产物带 hash 文件名)设一年 immutable 长缓存。location 内重复声明原有安全头,避免 nginx add_header 不继承导致安全头丢失。 - internal/router/router.go: Lite 版 serveFrontendStatic 按同样 策略设置 Cache-Control。 --- frontend/nginx.conf | 19 +++++++++++++++++++ internal/router/router.go | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/frontend/nginx.conf b/frontend/nginx.conf index ad114732..10cd79a6 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -19,6 +19,25 @@ server { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; + + # index.html 及 SPA fallback 不可缓存,否则前端升级后用户看不到新版本 + add_header Cache-Control "no-cache, must-revalidate" always; + # nginx add_header 不从上层继承,需要在 location 内重复声明安全头 + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-XSS-Protection "1; mode=block" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + } + + # Vite 构建产物 /assets/* 带 hash 文件名,可长期缓存 + location ^~ /assets/ { + root /usr/share/nginx/html; + + add_header Cache-Control "public, max-age=31536000, immutable" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-XSS-Protection "1; mode=block" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; } # 本地存储文件代理到后端服务(用于渲染 markdown 中的图片) diff --git a/internal/router/router.go b/internal/router/router.go index dbcd3737..5052b493 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -735,15 +735,28 @@ func serveFrontendStatic(r *gin.Engine) { } fullPath := filepath.Join(absDir, path) if info, err := os.Stat(fullPath); err == nil && !info.IsDir() { + setFrontendCacheHeaders(c.Writer, path) fileServer.ServeHTTP(c.Writer, c.Request) c.Abort() return } + setFrontendCacheHeaders(c.Writer, "/index.html") c.File(indexPath) c.Abort() }) } +// setFrontendCacheHeaders sets Cache-Control headers for frontend static resources. +// Vite 构建产物中 /assets/* 的文件名带 hash,可长期缓存;其余(index.html、config.js、favicon 等) +// 每次都需 revalidate,避免前端升级后用户看到旧版本。 +func setFrontendCacheHeaders(w http.ResponseWriter, path string) { + if strings.HasPrefix(path, "/assets/") { + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") + return + } + w.Header().Set("Cache-Control", "no-cache, must-revalidate") +} + // serveFiles serves files via query parameters and tenant storage settings. // It is registered after auth middleware, so tenant context comes from authentication. //