你應(yīng)該考慮的是如何實現(xiàn)文件版本號 hash 化而不是本末倒置去禁用緩存。
當(dāng)然,本地開發(fā)環(huán)境直接禁用瀏覽器緩存即可。
meta 標(biāo)簽的控制
// 栗子如下<meta http-equiv="cache-control" content="max-age=0" /><meta http-equiv="cache-control" content="no-cache" /><meta http-equiv="expires" content="0" /><meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /><meta http-equiv="pragma" content="no-cache" />
簡單總結(jié)一下我剛才查的資料
http-equiv=“pragma" 使用content屬性的no-cache值表示是否緩存網(wǎng)頁
http-equiv=“expires" content設(shè)為日期能夠控制緩存何時過期(注意必須使用GMT時間格式),等于0或-1禁用緩存瀏覽本站
對于各項cache-control的解釋,大家可以查看該鏈接
操作文件名使瀏覽器重新加載文件
可以在文件名后面加上MD5,時間戳,哈希值等隨機數(shù)
比如我使用的是webpack打包,此時可以在webpack.config.js中對output的文件名進行配置
output: { filename: "[name]-[hash:6].js” // 加入了哈希
}