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