vue、angular等index.html緩存問(wèn)題

本文引自

https://github.com/ant-design/ant-design-pro/issues/1365

瀏覽器中,默認(rèn)會(huì)對(duì) html css js 等靜態(tài)文件、以及重定向進(jìn)行緩存,如果在HEAD頭中指定:

<head>

<meta http-equiv="Expires" content="0">

<meta http-equiv="Pragma" content="no-cache">

<meta http-equiv="Cache-control" content="no-cache">

<meta http-equiv="Cache" content="no-cache">

</head>

瀏覽器不會(huì)緩存html,但是還是會(huì)對(duì)重定向緩存,并且這種方式并不規(guī)范,可能有的瀏覽器不支持。

解決方案是:

1) 對(duì)hash過(guò)的靜態(tài)文件還是采用默認(rèn)方式,客戶(hù)端會(huì)緩存。

2)對(duì)html文件,返回時(shí)增加頭:Cache-Control,必須每次來(lái)服務(wù)端校驗(yàn),根據(jù)etag返回200或者304

對(duì)應(yīng)的nginx配置如下:

server{

? ? listen? 80; #監(jiān)聽(tīng)端口

? ? server_name example.com

? ? # 前端靜態(tài)文件

? ? location ~* \.(gif|jpg|jpeg|png|css|js|ico|eot|otf|fon|font|ttf|ttc|woff|woff2)$ {

? ? ? ? root /var/www/example-fe/dist/;

? ? }

? ? # 前端html文件

? ? location / {

? ? ? ? # disable cache html

? ? ? ? add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';

? ? ? ? root /var/www/example-fe/dist/;

? ? ? ? index index.html index.htm;

? ? ? ? try_files $uri /index.html;

? ? }

}

由于瀏覽器緩存靜態(tài)文件的時(shí)間不可控,我們可以在nginx上自己配置expires 1M(1個(gè)月)

? ? # 前端靜態(tài)文件

? ? location ~* \.(gif|jpg|jpeg|png|css|js|ico|eot|otf|fon|font|ttf|ttc|woff|woff2)$ {

? ? ? ? root /var/www/example-fe/dist/;

? ? ? ? expires 1M;

? ? ? ? add_header Cache-Control "public";

? ? }

?著作權(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)容

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