![](/img/trans.png)
[英]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.