2017-03-17 79 views
0

許多人使用的CSS和JS文件,迫使非緩存版本的版本號來加載網頁上的時更新發布:如何版本號添加到HTML文件(不僅是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文件)從頭開始並且不使用緩存版本。

這是確保在所有網頁瀏覽器中從頭開始顯示HT​​ML文件的一種充分方法,或者有辦法將版本號設置爲HTML文件以確保新更新的HTML文檔沒有從瀏覽器的緩存中加載?

回答

2

通常瀏覽器總是會使用較新版本的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' 

的Python在瓶

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" 
resp.headers["Pragma"] = "no-cache" 
resp.headers["Expires"] = "0" 

谷歌去

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

相關問題