diff --git a/ui/ui.go b/ui/ui.go index c7d4ad1999850f86ff33a78979a5de23334d728b..51feb71cbeb6ac789976e85ee4e609b9d9158b23 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -72,10 +72,7 @@ func Build(config *Config, urls common.URLMap) (http.Handler, *common.Renderer, }))) // Add cache-friendly headers. - h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Cache-Control", "max-age=86400") - mux.ServeHTTP(w, req) - }) + h := withCacheHeaders(mux) return h, renderer, nil } @@ -134,3 +131,21 @@ func toJSON(obj interface{}) string { data, _ := json.Marshal(obj) return string(data) } + +type cachedResponseWriter struct { + http.ResponseWriter +} + +func (c *cachedResponseWriter) WriteHeader(statusCode int) { + if statusCode == http.StatusOK { + c.ResponseWriter.Header().Set("Cache-Control", "max-age=86400") + } + c.ResponseWriter.WriteHeader(statusCode) +} + +func withCacheHeaders(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + cw := &cachedResponseWriter{ResponseWriter: w} + h.ServeHTTP(cw, req) + }) +}