From 061261f032aed1d18054ef03c960762695e64aef Mon Sep 17 00:00:00 2001 From: Louis Garman Date: Tue, 11 Jul 2023 21:41:28 +0100 Subject: [PATCH] fix(ui): add cache-control header to static files --- internal/http/html/static.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/http/html/static.go b/internal/http/html/static.go index bf83fbf34..4dc49709e 100644 --- a/internal/http/html/static.go +++ b/internal/http/html/static.go @@ -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 }