From 8936f65c9032eb363ff936c6bbac5a94a348585d Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 15 Jul 2016 08:57:52 -0400 Subject: Improving caching of static assets (#3591) --- web/web.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'web/web.go') diff --git a/web/web.go b/web/web.go index 8e96edd69..1b50bf474 100644 --- a/web/web.go +++ b/web/web.go @@ -29,15 +29,22 @@ func InitWeb() { staticDir := utils.FindDir(CLIENT_DIR) l4g.Debug("Using client directory at %v", staticDir) if *utils.Cfg.ServiceSettings.WebserverMode == "gzip" { - mainrouter.PathPrefix("/static/").Handler(gziphandler.GzipHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir))))) + mainrouter.PathPrefix("/static/").Handler(gziphandler.GzipHandler(staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))))) } else { - mainrouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))) + mainrouter.PathPrefix("/static/").Handler(staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir))))) } mainrouter.Handle("/{anything:.*}", api.AppHandlerIndependent(root)).Methods("GET") } } +func staticHandler(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "max-age=31556926, public") + handler.ServeHTTP(w, r) + }) +} + var browsersNotSupported string = "MSIE/8;MSIE/9;MSIE/10;Internet Explorer/8;Internet Explorer/9;Internet Explorer/10;Safari/7;Safari/8" func CheckBrowserCompatability(c *api.Context, r *http.Request) bool { @@ -68,5 +75,6 @@ func root(c *api.Context, w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") http.ServeFile(w, r, utils.FindDir(CLIENT_DIR)+"root.html") } -- cgit v1.2.3-1-g7c22