2012-03-19 48 views
1

我在這裏有一個問題。如何告訴瀏覽器重新驗證緩存

我正在使用ETag來驗證瀏覽器緩存。但問題是,當我做window.location.pathname時,它傾向於從緩存中獲取頁面。但是當我在頁面上執行F5時,它會向服務器發送重新驗證請求。

即使當我做window.location.pathname時,我想重新驗證緩存。或者有更好的方法來做到這一點。

這是我的請求和響應頭。

Request URL:http://127.0.0.1:5555/monyog-license.html 
Request Method:GET 
Status Code:200 OK 
Request Headers 
Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:max-age=0 
Connection:keep-alive 
Cookie:SessionID=fe5b68c5c1f377a063462e59a67efb90; Username=admin; IsAdmin=1; HasServerEdit=1 
Host:127.0.0.1:5555 
Pragma:no-cache 
Referer:http://127.0.0.1:5555/monyog-login.html 
User-Agent:Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.630.0 Safari/534.16 
Response Headers 
Cache-Control:max-age=3600, must-revalidate 
Content-Encoding:gzip 
Content-Length:1401 
Content-type:text/html; charset=utf-8 
ETag:1331890257_3961 
Expires:Tue, 19 Mar 2013 07:34:04 GMT 
Last-Modified:Fri, 16 Mar 2012 09:30:57 GMT 

回答

1

這樣的事情如何:window.location.pathname + '?' + (+(new Date))

1

你在Apache上運行嗎?如果是這樣,你可能想使用.htaccess來控制緩存。以下是一些示例:

# 1 YEAR 
<filesMatch "\.(html|htm|php|cgi|pl)$"> 
Header set Cache-Control "max-age=29030400, public" 
</filesMatch> 

# 1 WEEK 
<filesMatch "\.(html|htm|php|cgi|pl)$"> 
Header set Cache-Control "max-age=604800, public" 
</filesMatch> 

# 3 HOUR 
<filesMatch "\.(html|htm|php|cgi|pl)$"> 
Header set Cache-Control "max-age=10800" 
</filesMatch> 

# NEVER CACHE 
<filesMatch "\.(html|htm|php|cgi|pl)$"> 
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate" 
</filesMatch> 

您可以通過將其添加到行來設置所需的任何文件類型。只要確保每個文件類型都用|分隔就像上面的例子一樣。

祝你好運!

最佳,

辛西婭