Enable browser caching of static resources

This sends the Cache-Control header to allow caching of static resources,
and checks the If-Modified-Since header to allow browser to use the
cached resource on refresh.
This commit is contained in:
Jakob Borg 2015-08-08 13:48:09 +02:00
parent 0bfcafc5c6
commit a71090df81

View File

@ -955,6 +955,11 @@ func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
if r.Header.Get("If-Modified-Since") == auto.AssetsBuildDate {
w.WriteHeader(http.StatusNotModified)
return
}
mtype := s.mimeTypeForFile(file) mtype := s.mimeTypeForFile(file)
if len(mtype) != 0 { if len(mtype) != 0 {
w.Header().Set("Content-Type", mtype) w.Header().Set("Content-Type", mtype)
@ -970,6 +975,7 @@ func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs))) w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
w.Header().Set("Last-Modified", auto.AssetsBuildDate) w.Header().Set("Last-Modified", auto.AssetsBuildDate)
w.Header().Set("Cache-Control", "public")
w.Write(bs) w.Write(bs)
} }