Skip to content
Snippets Groups Projects
Commit 2006464b authored by ale's avatar ale
Browse files

Only set Cache-Control header on status 200

parent e9eabef2
No related branches found
No related tags found
No related merge requests found
Pipeline #79519 passed
...@@ -72,10 +72,7 @@ func Build(config *Config, urls common.URLMap) (http.Handler, *common.Renderer, ...@@ -72,10 +72,7 @@ func Build(config *Config, urls common.URLMap) (http.Handler, *common.Renderer,
}))) })))
// Add cache-friendly headers. // Add cache-friendly headers.
h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { h := withCacheHeaders(mux)
w.Header().Set("Cache-Control", "max-age=86400")
mux.ServeHTTP(w, req)
})
return h, renderer, nil return h, renderer, nil
} }
...@@ -134,3 +131,21 @@ func toJSON(obj interface{}) string { ...@@ -134,3 +131,21 @@ func toJSON(obj interface{}) string {
data, _ := json.Marshal(obj) data, _ := json.Marshal(obj)
return string(data) 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)
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment