它是用作浏览器缓存键的文件名还是整个 URL? [英] Is it the filename or the whole URL used as a key in browser caches?

查看:30
本文介绍了它是用作浏览器缓存键的文件名还是整个 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常希望浏览器缓存资源 - JavaScript、CSS、图像等,直到有新版本可用,然后确保浏览器获取并缓存新版本.

It's common to want browsers to cache resources - JavaScript, CSS, images, etc. until there is a new version available, and then ensure that the browser fetches and caches the new version instead.

一个解决方案是在资源的文件名中嵌入一个版本号,但是将要以这种方式管理的资源放在一个包含修订号的目录中会做同样的事情吗?文件的整个 URL 是用作浏览器缓存中的键,还是只是文件名本身和一些元数据?

One solution is to embed a version number in the resource's filename, but will placing the resources to be managed in this way in a directory with a revision number in it do the same thing? Is the whole URL to the file used as a key in the browser's cache, or is it just the filename itself and some meta-data?

如果我的代码从获取 /r20/example.js 更改为 /r21/example.js,我是否可以确定 example.js 的修订版 20?js 已缓存,但现在已获取修订版 21,现在已缓存?

If my code changes from fetching /r20/example.js to /r21/example.js, can I be sure that revision 20 of example.js was cached, but now revision 21 has been fetched instead and it is now cached?

推荐答案

是的,URL 任何部分的任何更改(不包括 HTTP 和 HTTPS 协议更改)都被解释为不同的资源浏览器(和任何中间代理),因此会在浏览器缓存中产生一个单独的实体.

Yes, any change in any part of the URL (excluding HTTP and HTTPS protocols changes) is interpreted as a different resource by the browser (and any intermediary proxies), and will thus result in a separate entity in the browser-cache.

更新:

这篇 ThinkVitamin 文章中声称 Opera 和 Safari/Webkit浏览器不缓存 ?query=strings 为 false 的 URL.

The claim in this ThinkVitamin article that Opera and Safari/Webkit browsers don't cache URLs with ?query=strings is false.

向 URL 添加版本号参数是执行缓存破坏的一种完全可以接受的方式.

Adding a version number parameter to a URL is a perfectly acceptable way to do cache-busting.

可能让 ThinkVitamin 文章的作者感到困惑的是,在 Safari 和 Opera 的地址/位置栏中按 Enter 会导致包含查询字符串的 URL 的行为不同.

What may have confused the author of the ThinkVitamin article is the fact that hitting Enter in the address/location bar in Safari and Opera results in different behavior for URLs with query string in them.

但是,(这是重要的部分!)Opera 和 Safari 在缓存嵌入/链接的图像、样式表和脚本方面的行为与 IE 和 Firefox 一样在网页中 - 无论它们是否有?"URL 中的字符.(这可以通过在普通 Apache 服务器上的简单测试来验证.)

However, (and this is the important part!) Opera and Safari behave just like IE and Firefox when it comes to caching embedded/linked images and stylesheets and scripts in web pages - regardless of whether they have "?" characters in their URLs. (This can be verified with a simple test on a normal Apache server.)

(如果我有这样做的声誉,我会评论当前接受的答案.:-)

(I would have commented on the currently accepted answer if I had the reputation to do it. :-)

这篇关于它是用作浏览器缓存键的文件名还是整个 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆