Javascript 版本控制以避免缓存,这些做法有何不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8224736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript versioning to avoid caching, difference in these practices?
提问by DhruvPathak
If I decide to use last_modified_time of a javascript or css file, and use the unix timestamp of it as a key in the name to bust cache when file is modified. What is the difference between following two practices ? filename is : my_script.js and timestamp is : 1321951817
如果我决定使用 javascript 或 css 文件的 last_modified_time,并使用它的 unix 时间戳作为名称中的键,以在文件被修改时破坏缓存。以下两种做法有什么区别?文件名是:my_script.js,时间戳是:1321951817
1/ File gets included as :
<script type="text/javascript" src="http://example.com/js/my_script.js?v=1321951817"></script>
Hence,the query string parameter creates a new cache everytime the v
is changed.
1/ 文件被包含为:
<script type="text/javascript" src="http://example.com/js/my_script.js?v=1321951817"></script>
因此,每次v
更改时,查询字符串参数都会创建一个新缓存。
2/ File gets included as :
<script type="text/javascript" src="http://example.com/js/my_script.1321951817.js"></script>
The filename changes with every modification, a rewrite rule removes the timestamp and points the requested url to my_script.js
2/ 文件被包含为:
<script type="text/javascript" src="http://example.com/js/my_script.1321951817.js"></script>
文件名随每次修改而变化,重写规则删除时间戳并将请求的 url 指向my_script.js
3/ UPDATE: ONE MORE METHOD BASED ON ANSWERS BELOW: File is renamed and get included as :
<script type="text/javascript" src="http://example.com/js/my_script.1321951817.js"></script>
The filename is changed and NO REWRITE RULE is used.
3/更新:基于以下答案的另一种方法:文件已重命名并包含为:
<script type="text/javascript" src="http://example.com/js/my_script.1321951817.js"></script>
文件名已更改且未使用重写规则。
Question : Are these two techniques inherently the same, or are there any advantages/disadvantages of using query string parameters instead of direct file name.
问题:这两种技术本质上是否相同,或者使用查询字符串参数而不是直接文件名有什么优点/缺点。
回答by Munter
Using an updated querystring is a bad solution. See what Steve souders has so say about it: http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
使用更新的查询字符串是一个糟糕的解决方案。看看 Steve souders 怎么说:http: //www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
The ideal method is to rename the file itself. Some people prefer using the time stamp of the last modification date, which i think is a problem.
理想的方法是重命名文件本身。有些人更喜欢使用上次修改日期的时间戳,我认为这是一个问题。
In modern web development you really need to optimize your page as much as possible, which means combining css and javascript into single files which are minfied. That means that you introduce a build step into your process, and that the last modification time of your file always will be at your last build. If you set that as your file name, you essentially bust the users cache all the time, and some times you dont need to.
在现代 Web 开发中,您确实需要尽可能地优化您的页面,这意味着将 css 和 javascript 组合成单个文件并进行压缩。这意味着您在流程中引入了一个构建步骤,并且文件的最后修改时间始终是您的最后一次构建。如果您将其设置为您的文件名,您实际上一直在破坏用户缓存,有时您不需要。
I recommend renaming the files to an md5 sum of their content. That way you can do new builds all the time, but the file name only changes if the content changes. This makes your file name an identifier of the content. Using this you can set a far future expires header on all your static content and simply stop worrying about it any more.
我建议将文件重命名为其内容的 md5 总和。这样您就可以一直进行新的构建,但只有在内容发生变化时文件名才会发生变化。这使您的文件名成为内容的标识符。使用它,您可以在所有静态内容上设置一个远期到期标题,并且不再担心它。
I can recommend using a build system for this, since this workflow gets boring fast. My company open sourced one a while ago that does this among a lot of other things that optimize your web page: https://github.com/One-com/assetgraph-builderThere are many other build tools that do the same. Take a look around and find the one that best suits your development setup.
我可以推荐为此使用构建系统,因为这个工作流程很快就会变得无聊。我的公司不久前开源了一个,它在优化您的网页的许多其他事情 中做到了这一点:https: //github.com/One-com/assetgraph-builder还有许多其他构建工具可以做同样的事情。环顾四周,找到最适合您的开发设置的那个。
回答by baklap
You are saying it yourself: in the second example you are using a rewrite rule which checks with regex every page you are loading.
您是自己说的:在第二个示例中,您使用的是重写规则,该规则使用正则表达式检查您正在加载的每个页面。
The first one just fooling the browser to think it's a different file. So the first one is the way to go.
第一个只是愚弄浏览器认为它是一个不同的文件。所以第一个是要走的路。