dping1968 2018-03-15 18:30
浏览 37

设置了缓存头,但仍在发送网络请求

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么