【问题标题】:How to tell the browser to revalidate the cache如何告诉浏览器重新验证缓存
【发布时间】:2012-03-19 07:41:23
【问题描述】:

我这里有问题。

我正在使用 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

【问题讨论】:

    标签: javascript html browser browser-cache


    【解决方案1】:

    像这样的东西怎么样:window.location.pathname + '?' + (+(new Date))

    【讨论】:

      【解决方案2】:

      您在 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>
      

      您可以通过将其添加到该行来设置您想要的任何文件类型。只需确保每个文件类型用 | 分隔即可就像上面的例子一样。

      祝你好运!

      最好的,

      辛西娅

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-14
        • 2014-03-08
        • 2013-03-28
        • 2012-05-16
        • 2018-02-16
        • 2012-03-19
        • 2014-05-30
        • 1970-01-01
        相关资源
        最近更新 更多