feat(api): force no-cache on HTML files

pull/551/head
Anthony Lapenna 2017-01-26 11:45:03 +13:00
parent fcc9203416
commit 835b273700
1 changed files with 18 additions and 2 deletions

View File

@ -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)
}