I'm writing a golang server that serves images. When a request comes in I return the image and also set the HTTP Cache headers.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() == "/sw.js" || r.URL.String() == "/favicon.ico" {
return
}
// logic to get image
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Content-Length", strconv.FormatInt(*result.ContentLength, 10))
w.Header().Set("Cache-Control", "public, max-age=2592000")
io.Copy(w, result.Body)
})
Right now I'm just testing this out on my browser. I request an image and all of the headers are returned from the server correctly. I checked chrome://cache/
and the url is there with all the correct headers set.
However when click enter
again in the url address bar to re-request the image. The browser is still sending a network request to the server. Am I missing something from my response headers? I've also implemented this with etag header and just returning a http.StatusNotModified
response if the etag key hasn't changed. However I'm trying to avoid the network request all together.