Asking browsers to cache as aggressively as possible

后端 未结 7 901
既然无缘
既然无缘 2020-12-07 19:18

This is about a web app that serves images. Since the same request will always return the same image, I want the accessing browsers to cache the images as aggressively as po

7条回答
  •  甜味超标
    2020-12-07 19:43

    You can do better. 304s are still a HTTP request/response. Though the image is not downloaded again, the latency can be killing.

    If you can include a version identifier in your image names, you can set the max-age to 2 years. That way, you prevent 304s. If the image ever changes, you update the version identifier thereby changing the file name. This ensures that the browser will issue a fresh request.

    It needs some changes to your project structure. The version identifier can be the SVN revision number when the image was last updated, and can be auto-generated at build time. You'd also need to update the html, so if you have a logical mapping between image name and image path, your job would be easier.

    Images are rarely updated, so you could also follow a manual approach if you can't automate what I described above. The trick is to only add new images, never modify them.

提交回复
热议问题