Skip to content

Commit

Permalink
bskyweb: do not serve cache headers on non-2XX for static assets (#7469)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy authored Jan 17, 2025
1 parent c84158f commit ec9cafd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions bskyweb/cmd/bskyweb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit ec9cafd

Please sign in to comment.