如何在客户端缓存文件,以便浏览器甚至不再费心再次请求它?
我发回带有以下 HTTP 响应标头的图像:
Cache-Control: private,max-age=86400
我的理解是,浏览器在 24 小时内不应该请求该文件(86,400 = 60s * 60m * 24h)。
我在后续请求中看到的是,它仍然要求提供该文件,但返回“304 Not Modified”。这很好,但我什至想删除该请求/响应。
需要什么标头才能防止浏览器甚至懒得去请求该文件,而只是让它盲目地使用本地缓存中的文件,直到该文件过期?
I send back an image with the following HTTP response header:
Cache-Control: private,max-age=86400
My understanding is that the browser should not even ask for this file for 24 hours (86,400 = 60s * 60m * 24h).
What I'm seeing on subsequent requests is that it still asks for the file, but gets back a "304 Not Modified." This is good, but I want to remove even that request/response.
What header is required to prevent the browser from even bothering to ask for the file, and just have it blindly use the file it has in local cache, until that file expires?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全取决于您如何测试。在 Firefox 3.6 和 IE8 上,单击一个链接,然后单击一个将您移回首页的链接,将正确使用 max-age 的缓存。在 URL 字段中再次点击 Return 键将显示相同的行为。
但是,按 F5 将再次询问该文件,但允许 304 响应。
按 Ctrl+F5 将始终再次请求该文件,并将 Cache-Control 和 Pragma 设置为 no-cache,强制返回 200 响应。
It all really depends on how you're testing this. On Firefox 3.6 and IE8, clicking on a link and then on a link that move you back to the first page will use the cache correctly with max-age. Hitting the Return key again in the URL field will show the same behavior.
However, hitting F5 will ask again for the file but allows 304 responses.
Hitting Ctrl+F5 will always ask again for the file, with Cache-Control and Pragma set to no-cache, forcing a 200 response.
这根本无法在 HTML < 中可靠地完成。 5.
您可以在 HTML5 或使用浏览器扩展程序(例如 Gears)来提供此功能。
This simply can't be done reliably in HTML < 5.
You could use client side storage in HTML5 or use a browser extension such as Gears to provide this functionality.