Skip to content

Commit

Permalink
Add Cache-Control max age 30 days to /assets
Browse files Browse the repository at this point in the history
If browser has a cache version of assest, do not check server
since vite add a hash to all assets, every change in the frontend generate other file names and invalidate the cache.
  • Loading branch information
StefanSchoof committed Dec 4, 2023
1 parent dc650fd commit 713e7ce
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ func NewHTTPd(addr string, hub *SocketHub) *HTTPd {
})

static.HandleFunc("/", indexHandler())
for _, dir := range []string{"assets", "meta"} {
static.PathPrefix("/" + dir).Handler(http.FileServer(http.FS(assets.Web)))
}
static.PathPrefix("/assets").Handler(CacheControlWrapper(http.FileServer(http.FS(assets.Web))))
static.PathPrefix("/meta").Handler(http.FileServer(http.FS(assets.Web)))
static.PathPrefix("/i18n").Handler(http.StripPrefix("/i18n", http.FileServer(http.FS(assets.I18n))))

srv := &HTTPd{
Expand All @@ -76,6 +75,13 @@ func NewHTTPd(addr string, hub *SocketHub) *HTTPd {
return srv
}

func CacheControlWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
h.ServeHTTP(w, r)
})
}

// Router returns the main router
func (s *HTTPd) Router() *mux.Router {
return s.Handler.(*mux.Router)
Expand Down

0 comments on commit 713e7ce

Please sign in to comment.