Vue如何解決每次發(fā)版都要強(qiáng)刷清除瀏覽器緩存問題
每次發(fā)版都要強(qiáng)刷清除瀏覽器緩存問題
原理
將打包后的js和css文件,加上打包時的時間戳
1.index.html
在 public 目錄下的index.html文件里添加如下代碼:
<meta http-equiv="pragram" content="no-cache"> <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="expires" content="0">
2.vue.config.js
在vue.config.js中,配置相關(guān)打包配置,代碼如下:
let timeStamp = new Date().getTime();
module.exports = {
? ? publicPath: "./",
? ? filenameHashing: false,
? ? // 打包配置
? ? configureWebpack: {
? ? ? ? output: { // 輸出重構(gòu) 打包編譯后的js文件名稱,添加時間戳.
? ? ? ? ? ? filename: `js/js[name].${timeStamp}.js`,
? ? ? ? ? ? chunkFilename: `js/chunk.[id].${timeStamp}.js`,
? ? ? ? }
? ? },
? ? css: {
? ? ? ? extract: { // 打包后css文件名稱添加時間戳
? ? ? ? ? ? filename: `css/[name].${timeStamp}.css`,
? ? ? ? ? ? chunkFilename: `css/chunk.[id].${timeStamp}.css`,
? ? ? ? }
? ? }
};filename指列在entry 中,打包后輸出的文件的名稱。chunkFilename指未列在entry 中,卻又需要被打包出來的文件的名稱。
vue 強(qiáng)制清除瀏覽器緩存
(1)最基本的方法就是
在打包的時候給每個打包文件加上hash 值,一般是在文件后面加上時間戳
//在vue.config.js 文件中,找到output:
const Timestamp = new Date().getTime()
output: { // 輸出重構(gòu) ?打包編譯后的 文件名稱 ?【模塊名稱.版本號.時間戳】
? ? ? filename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js`,
? ? ? chunkFilename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js`
?
? ? }(2)在html文件中加入meta標(biāo)簽
(不推薦此方法)
<meta http-equiv="pragram" content="no-cache"> <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
(3)需要后端陪著,進(jìn)行nginx配置
location = /index.html {
? ? add_header Cache-Control "no-cache, no-store";
}原因: 第二種方法瀏覽器也會出現(xiàn)緩存,配置之后禁止html 出現(xiàn)緩存
no-cache, no-store可以只設(shè)置一個
no-cache瀏覽器會緩存,但刷新頁面或者重新打開時 會請求服務(wù)器,服務(wù)器可以響應(yīng)304,如果文件有改動就會響應(yīng)200no-store瀏覽器不緩存,刷新頁面需要重新下載頁面
(4)在腳本加載時加入一個時間戳
修改 webpack.prod.conf.js 文件。(未使用過該方法,需要實(shí)踐)
const version = new Date().getTime();
new HtmlWebpackPlugin({
? ? filename: config.build.index,
? ? template: 'index.html',
? ? inject: true,
? ? hash: version,
? ? favicon: resolve('icon.ico'),
? ? title: 'vue-admin-template',
? ? minify: {
? ? ? ? removeComments: true,
? ? ? ? collapseWhitespace: true,
? ? ? ? removeAttributeQuotes: true
? ? }
})以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+koa實(shí)現(xiàn)文件上傳功能的全過程記錄
開發(fā)項(xiàng)目的時候,用到文件上傳的功能很常見,包括單文件上傳和多文件上傳,下面這篇文章主要給大家介紹了關(guān)于vue3+koa實(shí)現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue實(shí)現(xiàn)移動端輕量日期組件不依賴第三方庫的方法
這篇文章主要介紹了vue 移動端輕量日期組件不依賴第三方庫,需要的朋友可以參考下2019-04-04
理解Proxy及使用Proxy實(shí)現(xiàn)vue數(shù)據(jù)雙向綁定操作
這篇文章主要介紹了理解Proxy及使用Proxy實(shí)現(xiàn)vue數(shù)據(jù)雙向綁定操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

