许多人在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文件)刮擦而不使用缓存版本。
这是确保在所有网络浏览器中从头开始显示HTML文件的充分方法,还是有办法将版本号设置为 HTML文件,以确保新的更新的HTML文档不会从浏览器的缓存中加载?
答案 0 :(得分:3)
通常,浏览器始终使用较新版本的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');
ASP
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'
Flask上的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