From 9a9610c2a9a0e150e0bb49309a87f305fbe399ca Mon Sep 17 00:00:00 2001 From: Remington Breeze Date: Mon, 3 Jun 2024 14:33:56 -0700 Subject: [PATCH] chore(ui): add cache headers for static assets Signed-off-by: Remington Breeze --- internal/api/server.go | 1 + internal/http/http.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/internal/api/server.go b/internal/api/server.go index 170551479..eb40a72a6 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -319,6 +319,7 @@ func newDashboardRequestHandler() (http.HandlerFunc, error) { } // Path is a file + httputil.SetCacheHeaders(w, 30*24*time.Hour, 7*24*time.Hour) handler.ServeHTTP(w, req) }) diff --git a/internal/http/http.go b/internal/http/http.go index e969052e2..03f097645 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -20,3 +20,11 @@ func SetNoCacheHeaders(w http.ResponseWriter) { w.Header().Set(k, v) } } + +func SetCacheHeaders(w http.ResponseWriter, maxAge time.Duration, timeUntilExpiry time.Duration) { + if w == nil { + return + } + w.Header().Set("Cache-Control", "public, max-age="+maxAge.String()) + w.Header().Set("Expires", time.Now().Add(timeUntilExpiry).Format(time.RFC1123)) +}