[英]Add a comment with date or an incrementing “version” number to js, css or html files using a preprocessor?
[英]How to add version number to HTML file (not only to css and js files)
许多人使用CSS和JS文件上的版本号来强制非缓存版本在发布更新时加载到网页上:
CSS范例:
<link rel="stylesheet" type="text/css" href="style.css?v=2017-03-17">
JS范例:
<script type="text/javascript" src="js/myscript.js?v=2017-03-17"></script>
我注意到,通过将版本号添加到css / js文件中,Web浏览器还将从头开始加载HTML文件 (从中引用css / js文件),而不使用缓存的版本。
这是确保在所有Web浏览器中从头开始显示HTML文件的充分方法,还是有办法为HTML文件设置版本号,以确保不会从浏览器的浏览器中加载新更新的HTML文档。缓存?
通常,浏览器始终采用HTML的较新版本。
如果您希望防止任何缓存,可以使用以下技巧:
适用于最重要的浏览器的正确的最小标头集:
Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0
的HTML
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" />
.htaccess(Apache)
<IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires 0 </IfModule>
Java Servlet
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0);
的PHP
header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');
均价
Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" Response.addHeader "Pragma", "no-cache" Response.addHeader "Expires", "0"
ASP.NET
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");
Ruby on Rails
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '0'
烧瓶上的Python
resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" resp.headers["Pragma"] = "no-cache" resp.headers["Expires"] = "0"
Google Go
responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") responseWriter.Header().Set("Pragma", "no-cache") responseWriter.Header().Set("Expires", "0")
来源: http : //cristian.sulea.net/blog.php?p=2014-01-14-disable-browser-caching-with-meta-html-tags
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.