(譯)使用 meta 禁用瀏覽器緩存

原文

一個(gè)最簡(jiǎn)單的支持大部分主流瀏覽器的 headers 集如下:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
  • Cache-Control 作用于 HTTP1.1

HTTP1.1中啟用Cache-Control 來(lái)控制頁(yè)面的緩存與否,這里介紹幾個(gè)常用的參數(shù):

  • no-cache,瀏覽器和緩存服務(wù)器都不應(yīng)該緩存頁(yè)面信息;
  • public,瀏覽器和緩存服務(wù)器都可以緩存頁(yè)面信息;
  • no-store,請(qǐng)求和響應(yīng)的信息都不應(yīng)該被存儲(chǔ)在對(duì)方的磁盤(pán)系統(tǒng)中;
  • must-revalidate,對(duì)于客戶機(jī)的每次請(qǐng)求,代理服務(wù)器必須想服務(wù)器驗(yàn)證緩存是否過(guò)時(shí)
  • Pragma 作用于 HTTP 1.0

HTTP1.0 中通過(guò) Pragma 控制頁(yè)面緩存,通常設(shè)置的值為 no- cache,不過(guò)這個(gè)值不這么保險(xiǎn),通常還加上 Expires 置為 0 來(lái)達(dá)到目的。但是如我們刻意需要瀏覽器或緩存服務(wù)器緩存住我們的頁(yè)面這個(gè)值則要設(shè)置為 Pragma。

  • Expires 作用于 proxies

表示存在時(shí)間,允許客戶端在這個(gè)時(shí)間之前不去檢查(發(fā)請(qǐng)求),等同 max-age 的 效果。但是如果同時(shí)存在,則被 Cache-Controlmax-age 覆蓋。 格式: Expires :時(shí)間,后面跟一個(gè)時(shí)間或者日期,超過(guò)這個(gè)時(shí)間后緩存失效。也就是瀏覽器發(fā)出請(qǐng)求之前,會(huì)檢查這個(gè)時(shí)間是否失效,若失效,則瀏覽器會(huì)重新發(fā)出請(qǐng)求。

HTML

通過(guò)添加 <meta> 標(biāo)簽來(lái)禁止瀏覽器緩存(代碼必須包含在 <head> 標(biāo)簽中)

<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" />

其他環(huán)境的設(shè)置

  • .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 on Flask
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")

Resources

后記

上述方案對(duì)于 Safari 并無(wú)鳥(niǎo)用,一種比較正規(guī)的方案是:

    $(window).bind("pageshow", function (event) {
        if (event.originalEvent.persisted) {
            window.location.reload();
        }
    });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,774評(píng)論 1 45
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • php.ini設(shè)置,上傳大文件: post_max_size = 128Mupload_max_filesize ...
    bycall閱讀 6,997評(píng)論 3 64
  • 淺談瀏覽器Http的緩存機(jī)制 ? ? ? ? ? ? ? ? 針對(duì)瀏覽器的http緩存的分析也算是老生常談了,每隔...
    meng_philip123閱讀 1,127評(píng)論 0 10
  • 針對(duì)瀏覽器的http緩存的分析也算是老生常談了,每隔一段時(shí)間就會(huì)冒出一篇不錯(cuò)的文章,其原理也是各大公司面試時(shí)幾乎必...
    全端玩法閱讀 964評(píng)論 0 9

友情鏈接更多精彩內(nèi)容