about summary refs log tree commit diff
path: root/bskyweb
diff options
context:
space:
mode:
authordevin ivy <devinivy@gmail.com>2025-01-17 12:36:58 -0500
committerGitHub <noreply@github.com>2025-01-17 17:36:58 +0000
commitec9cafdbbd476228b7ead5360c792c7d37cc5660 (patch)
tree4585b2c3d28233a6fff5a45a7d3922e20277194f /bskyweb
parentc84158fa8b7976cd5fcbee6f5f1863ef85f0b169 (diff)
downloadvoidsky-ec9cafdbbd476228b7ead5360c792c7d37cc5660.tar.zst
bskyweb: do not serve cache headers on non-2XX for static assets (#7469)
Diffstat (limited to 'bskyweb')
-rw-r--r--bskyweb/cmd/bskyweb/server.go20
1 files changed, 13 insertions, 7 deletions
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)
 		}
 	})