From ec9cafdbbd476228b7ead5360c792c7d37cc5660 Mon Sep 17 00:00:00 2001 From: devin ivy Date: Fri, 17 Jan 2025 12:36:58 -0500 Subject: bskyweb: do not serve cache headers on non-2XX for static assets (#7469) --- bskyweb/cmd/bskyweb/server.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'bskyweb') diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index 3ef19fc46..8792f553b 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -214,15 +214,21 @@ func serve(cctx *cli.Context) error { e.GET("/iframe/youtube.html", echo.WrapHandler(staticHandler)) e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", staticHandler)), func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - path := c.Request().URL.Path - maxAge := 1 * (60 * 60) // default is 1 hour + c.Response().Before(func() { + if c.Response().Status >= 300 { + return + } - // all assets in /static/js, /static/css, /static/media are content-hashed and can be cached for a long time - if strings.HasPrefix(path, "/static/js/") || strings.HasPrefix(path, "/static/css/") || strings.HasPrefix(path, "/static/media/") { - maxAge = 365 * (60 * 60 * 24) // 1 year - } + path := c.Request().URL.Path + maxAge := 1 * (60 * 60) // default is 1 hour + + // all assets in /static/js, /static/css, /static/media are content-hashed and can be cached for a long time + if strings.HasPrefix(path, "/static/js/") || strings.HasPrefix(path, "/static/css/") || strings.HasPrefix(path, "/static/media/") { + maxAge = 365 * (60 * 60 * 24) // 1 year + } - c.Response().Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge)) + c.Response().Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge)) + }) return next(c) } }) -- cgit 1.4.1