var epoch = time.Unix(0, 0).Format(time.RFC1123)
var headers = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
log.Errorln("no cache header")
for k, v := range headers {
rw.Header().Set(k, v)
}
http.ServeFile(rw, req, path)
I have the above code block in my server side to serve file requests. But the files served are still cached in browser.
But instead of http.ServeFile()
I used http.ServeContent()
with the last modified timestamp as time.Now()
. This works if the request has considerable time difference. Does browser give priority to last modified time than no-cache
header? Then what is the purpose of no-cache
header? Am I doing anything wrong?
Some browsers cache files without paying attention to headers. Typical solution is to add some get parameter to static file path, so the browser considered it like different request. It can be hash-sum or last modified timestamp. It might look somethind like this: site.com/icon.png?q=123456
.