From 588cc8398733402053d3266ead7d6efe2e638eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E9=9B=A8=E6=99=A8?= Date: Sat, 23 May 2026 02:47:51 +0800 Subject: [PATCH] fix: Cache middleware only caches static assets, not HTML pages The Cache middleware was setting max-age=604800 on ALL non-root responses, including HTML pages and 302 redirects. This caused Chrome to cache 302 redirect mappings persistently (this cache is NOT controlled by DevTools 'Disable cache'). When the system theme changed or different server instances had different themes, cached redirects in opposite directions created an infinite loop. Fix: Only set max-age=604800 for static assets (JS, CSS, fonts, images). HTML pages and other routes get no-cache to prevent stale redirects. --- middleware/cache.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/middleware/cache.go b/middleware/cache.go index 1a9dff877d9..c3d363230fc 100644 --- a/middleware/cache.go +++ b/middleware/cache.go @@ -1,17 +1,34 @@ package middleware import ( + "strings" + "github.com/gin-gonic/gin" ) func Cache() func(c *gin.Context) { return func(c *gin.Context) { - if c.Request.RequestURI == "/" { - c.Header("Cache-Control", "no-cache") + path := c.Request.URL.Path + if isStaticAsset(path) { + c.Header("Cache-Control", "max-age=604800") } else { - c.Header("Cache-Control", "max-age=604800") // one week + c.Header("Cache-Control", "no-cache") } c.Header("Cache-Version", "b688f2fb5be447c25e5aa3bd063087a83db32a288bf6a4f35f2d8db310e40b14") c.Next() } } + +func isStaticAsset(path string) bool { + if path == "/" { + return false + } + staticExtensions := []string{".js", ".css", ".woff", ".woff2", ".ttf", ".eot", + ".svg", ".png", ".jpg", ".jpeg", ".gif", ".ico", ".webp", ".map", ".webmanifest"} + for _, ext := range staticExtensions { + if strings.HasSuffix(path, ext) { + return true + } + } + return false +}