From 835b273700acd2d3ecba73e1595ba5d6295b577c Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Thu, 26 Jan 2017 11:45:03 +1300 Subject: [PATCH] feat(api): force no-cache on HTML files --- api/http/file_handler.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/api/http/file_handler.go b/api/http/file_handler.go index 95ebc022c..09a849b7c 100644 --- a/api/http/file_handler.go +++ b/api/http/file_handler.go @@ -1,6 +1,9 @@ package http -import "net/http" +import ( + "net/http" + "strings" +) // FileHandler represents an HTTP API handler for managing static files. type FileHandler struct { @@ -14,7 +17,20 @@ func newFileHandler(assetPath string) *FileHandler { return h } +func isHTML(acceptContent []string) bool { + for _, accept := range acceptContent { + if strings.Contains(accept, "text/html") { + return true + } + } + return false +} + func (fileHandler *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "max-age=31536000") + if !isHTML(r.Header["Accept"]) { + w.Header().Set("Cache-Control", "max-age=31536000") + } else { + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + } fileHandler.Handler.ServeHTTP(w, r) }