什么是 style.css?ver=1 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1614429/
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
what is style.css?ver=1 tag?
提问by Moon
I found out that some websites use css tag like style.css?ver=1
. What is this?
我发现有些网站使用像 .css 这样的 css 标签style.css?ver=1
。这是什么?
What is purpose of ?ver=1
?
的目的是?ver=1
什么?
How do I do it in code?
我如何在代码中做到这一点?
回答by Michal M
To avoid caching of CSS.
If the website updates their CSS they update the ver to a higher number, therefore browser is forced to get a new file and not use cached previous version.
Otherwise a browser may get a new HTML code and old CSS and some elements of the website may look broken.
避免缓存CSS。
如果网站更新了他们的 CSS,他们会将 ver 更新为更高的数字,因此浏览器将被迫获取一个新文件,而不是使用缓存的先前版本。
否则,浏览器可能会获得新的 HTML 代码和旧的 CSS,并且网站的某些元素可能看起来已损坏。
回答by Craig Trader
Adding '?ver=1' makes the HTTP request look like a GET query with parameters, and well-behaved browsers (and proxies) will refuse to cache parameterized queries. Of course well-behaved browsers (and proxies) should also pay attention to the 'Cache-control: no-cache', 'Expires', 'Last-Modified', and 'ETag' response headers (all of which were added to HTTP to specify correct caching behavior).
添加 '?ver=1' 使 HTTP 请求看起来像一个带有参数的 GET 查询,并且表现良好的浏览器(和代理)将拒绝缓存参数化查询。当然,行为良好的浏览器(和代理)也应该注意 'Cache-control: no-cache'、'Expires'、'Last-Modified' 和 'ETag' 响应头(所有这些都被添加到 HTTP指定正确的缓存行为)。
The '?ver=1' method is an expensive way to force behavior when the site developer doesn't know how (or is too lazy) to implement the correct response headers. In particular, it means that every page request is going to force requesting that CSS file, even though, in practice, CSS files change rarely, if at all.
当站点开发人员不知道如何(或太懒惰)实现正确的响应标头时,'?ver=1' 方法是一种强制行为的昂贵方法。特别是,这意味着每个页面请求都将强制请求该 CSS 文件,即使在实践中,CSS 文件很少更改,如果有的话。
My recommendation? Don't do it.
我的推荐? 不要这样做。
回答by Joseph
The purpose of the ?ver=1 is to parameterize the css file, so when they publish a new style.css file they up the version and it forces the client to download the new file, instead of pulling from the cached version.
?ver=1 的目的是参数化 css 文件,所以当他们发布一个新的 style.css 文件时,他们会升级版本并强制客户端下载新文件,而不是从缓存版本中提取。
回答by devpl
I think that ?ver=1
is for the version no. of the web app. Every time a new build is created, the app can update the ver
to the new version. This is so that the browser will load the new CSS file and not use the cached one (both use different file names).
我认为这?ver=1
是版本号。的网络应用程序。每次创建ver
新版本时,应用程序都可以将其更新到新版本。这是为了让浏览器加载新的 CSS 文件而不使用缓存的文件(两者使用不同的文件名)。
You can refer to this site: http://www.knowlegezone.com/36/article/Technology/Software/JavaScript/CSS-Caching-Hack----javascript-as-well
可以参考这个网站:http: //www.knowlegezone.com/36/article/Technology/Software/JavaScript/CSS-Caching-Hack----javascript-as-well
回答by devpl
If you are developing a web application in HTML and CSS or any other technology, and you are using some external CSS or JS files, you might notice one thing that in some cases if you made any changes to your existing .css or .js files then the browsers are not reflecting the changes immediately.
如果您正在使用 HTML 和 CSS 或任何其他技术开发 Web 应用程序,并且您正在使用一些外部 CSS 或 JS 文件,您可能会注意到一件事,在某些情况下,如果您对现有的 .css 或 .js 文件进行了任何更改那么浏览器不会立即反映更改。
What happens in that case is that the browser do not download a fresh copy of the latest version of the .css and .js files, instead it uses those files stored in your local cache. As a result the changes you made recently are not visible to you.
在这种情况下,浏览器不会下载最新版本的 .css 和 .js 文件的新副本,而是使用存储在本地缓存中的这些文件。因此,您最近所做的更改对您不可见。
<link rel="stylesheet" href="style.css?v=1.1">
The above case when you load the web page the browser will treat "style.css" as a different file along with "?v=1.1". Hence the browser is forced to download a fresh copy if the stylesheet or the script file.
上述情况,当您加载网页时,浏览器会将“style.css”与“?v=1.1”视为不同的文件。因此,如果样式表或脚本文件,浏览器将被迫下载一个新的副本。
回答by spig
IMO a better way to do this would be to include a hash generated off of the file size or a checksum based on the file contents or last-modified date. That way you don't have to update some version number and just let the number be driven off of the file's changing properties.
IMO 更好的方法是包含从文件大小或基于文件内容或上次修改日期的校验和生成的哈希。这样您就不必更新某些版本号,而只需让该编号脱离文件的更改属性即可。