Vue 項目 強制清除瀏覽器的緩存


(1)最基本的方法就是,在打包的時候給每個打包文件加上hash 值,一般是在文件后面加上時間戳

//在vue.config.js 文件中,找到output:
const Timestamp = new Date().getTime()
output: { // 輸出重構  打包編譯后的 文件名稱  【模塊名稱.版本號.時間戳】
      filename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js`,
      chunkFilename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js`

    }

(2)在html 文件中加入meta 標簽(不推薦此方法)

<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)需要后端陪着,進行  nginx 配置 

location = /index.html {
    add_header Cache-Control "no-cache, no-store";
}
原因: 第二種方法瀏覽器也會出現緩存,配置之后禁止html 出現緩存
no-cache, no-store可以只設置一個 
no-cache瀏覽器會緩存,但刷新頁面或者重新打開時 會請求服務器,服務器可以響應304,如果文件有改動就會響應200 
no-store瀏覽器不緩存,刷新頁面需要重新下載頁面

(4)在腳本加載時加入一個時間戳,修改 webpack.prod.conf.js 文件。(未使用過該方法,需要實踐)

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
	}
})

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM