Skip to content

Commit

Permalink
fix(ui): add cache-control header to static files
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Jul 11, 2023
1 parent 1da3936 commit 061261f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/http/html/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ func AddStaticHandler(logger logr.Logger, r *mux.Router, devMode bool) error {
} else {
fs = &cacheBuster{embedded}
}

r = r.NewRoute().Subrouter()

// Middleware to add cache control headers
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Instruct browser to cache static content for a very long time (1
// year), and rely on the cache buster to insert a hash to each
// requested URL, ensuring any content change invalidates the cache.
w.Header().Set("Cache-Control", "max-age=31536000")
next.ServeHTTP(w, r)
})
})
r.PathPrefix("/static/").Handler(http.FileServer(fs)).Methods("GET")
return nil
}

0 comments on commit 061261f

Please sign in to comment.