将脚本版本放在哪里有什么不同吗?查询参数或路径。利弊?
最佳答案
是的,有区别!
您需要将其作为文件名的一部分,否则某些浏览器不会缓存它。
发件人: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/