From 0e93d7815b5036f1c600a3a8fae738e16550d666 Mon Sep 17 00:00:00 2001 From: Node <8974108+qwenode@users.noreply.github.com> Date: Thu, 9 Nov 2023 10:44:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E9=9D=99=E6=80=81=E8=B5=84=E6=BA=90/assets/=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E7=BC=93=E5=AD=98=20(#2859)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What this PR does / why we need it? 当前操作控制台(每点击一个不同的页面)或刷新后,都需要从服务器加载静态资源且没有缓存,在网络差的情况下访问特别慢 #### Summary of your change 给静态资源增加浏览器缓存时间 #### Please indicate you've done the following: - [x] Made sure tests are passing and test coverage is added if needed. - [x] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/). - [x] Considered the docs impact and opened a new docs issue or PR with docs changes if needed. --- backend/init/router/router.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backend/init/router/router.go b/backend/init/router/router.go index 4517f40f9..ebcd39b8a 100644 --- a/backend/init/router/router.go +++ b/backend/init/router/router.go @@ -4,6 +4,7 @@ import ( "github.com/gin-contrib/gzip" "html/template" "net/http" + "strings" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/i18n" @@ -19,6 +20,12 @@ import ( func setWebStatic(rootRouter *gin.RouterGroup) { rootRouter.StaticFS("/public", http.FS(web.Favicon)) + rootRouter.Use(func(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, "/assets/") { + c.Header("Cache-Control", "max-age=31536000") + } + c.Next() + }) rootRouter.GET("/assets/*filepath", func(c *gin.Context) { staticServer := http.FileServer(http.FS(web.Assets)) staticServer.ServeHTTP(c.Writer, c.Request)