javascript - 缓存:myscript-1125.js vs myscript.js?1125

标签 javascript css http caching

将脚本版本放在哪里有什么不同吗?查询参数或路径。利弊?

最佳答案

是的,有区别!

您需要将其作为文件名的一部分,否则某些浏览器不会缓存它。

发件人:http://thinkvitamin.com/code/serving-javascript-fast/

According the letter of the HTTP caching specification, user agents should never cache URLs with query strings. While Internet Explorer and Firefox ignore this, Opera and Safari don’t – to make sure all user agents can cache your resources, we need to keep query strings out of their URLs

发件人:http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

Jacob Hoffman-Andrews, mentioned that Squid, a popular proxy, doesn’t cache resources with a querystring. This hurts performance when multiple users behind a proxy cache request the same file – rather than using the cached version everybody would have to send a request to the origin server.

(注意 Squid 中的默认行为有 since changed )

最后,有一个很棒的小服务可以让您“ping”Internet 上的一个页面并验证文件(及其使用的资源)的“可缓存性”

Cacheability - 检查 www.stackoverflow.com's Cacheability

您会注意到 StackOverflow 提供的 CSS 和 JS 文件在查询字符串中进行了版本控制,因此服务报告:

This object will be fresh for 1 week. It can be validated with Last-Modified. Because it contains a query ('?') in the URL, many caches will not keep this object.

关于javascript - 缓存:myscript-1125.js vs myscript.js?1125,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4460165/

相关文章:

javascript - 如何制作具有简单视差效果的 Bootstrap 轮播?

html - Internet Explorer 和 Google Chrome 的内容显示方式不同

html - 如何用两种颜色为方形 div 着色?

http - Woocommerce 核心更改外部链接协议(protocol)

javascript - if else 在react.js中(基本)

javascript - 如何从 JavaScript 中检索 GET 参数

css - 在 AngularJS 中调整元素高度

php - 检测用户是否在代理后面

javascript - 始终传递 http 身份验证 header javascript

javascript - HTML 输入类型 ="date"值格式