Merge pull request #1817 from mxinden/disable-caching

ui/web: Set HTTP headers to prevent asset caching
This commit is contained in:
Max Inden 2019-04-03 14:00:26 +02:00 committed by GitHub
commit a36bc1bb8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,24 +31,32 @@ func Register(r *route.Router, reloadCh chan<- chan error, logger log.Logger) {
r.Get("/metrics", promhttp.Handler().ServeHTTP)
r.Get("/", func(w http.ResponseWriter, req *http.Request) {
disableCaching(w)
req.URL.Path = "/static/"
fs := http.FileServer(asset.Assets)
fs.ServeHTTP(w, req)
})
r.Get("/script.js", func(w http.ResponseWriter, req *http.Request) {
disableCaching(w)
req.URL.Path = "/static/script.js"
fs := http.FileServer(asset.Assets)
fs.ServeHTTP(w, req)
})
r.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
disableCaching(w)
req.URL.Path = "/static/favicon.ico"
fs := http.FileServer(asset.Assets)
fs.ServeHTTP(w, req)
})
r.Get("/lib/*path", func(w http.ResponseWriter, req *http.Request) {
disableCaching(w)
req.URL.Path = path.Join("/static/lib", route.Param(req.Context(), "path"))
fs := http.FileServer(asset.Assets)
fs.ServeHTTP(w, req)
@ -76,3 +84,9 @@ func Register(r *route.Router, reloadCh chan<- chan error, logger log.Logger) {
r.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
r.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
}
func disableCaching(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0") // Prevent proxies from caching.
}