目录每次发版都要强刷清除浏览器缓存问题原理Vue 强制清除浏览器缓存(1)最基本的方法就是(2)在html文件中加入meta标签(3)需要后端陪着,进行Nginx配置 (4)在脚本加
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中,配置相关打包配置,代码如下:
let timeStamp = new Date().getTime();
module.exports = {
publicPath: "./",
filenameHashing: false,
// 打包配置
configurewebpack: {
output: { // 输出重构 打包编译后的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 中,却又需要被打包出来的文件的名称。在打包的时候给每个打包文件加上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`
}
(不推荐此方法)
<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">
location = /index.html {
add_header Cache-Control "no-cache, no-store";
}
原因: 第二种方法浏览器也会出现缓存,配置之后禁止html 出现缓存
no-cache, no-store可以只设置一个
no-cache
浏览器会缓存,但刷新页面或者重新打开时 会请求服务器,服务器可以响应304,如果文件有改动就会响应200no-store
浏览器不缓存,刷新页面需要重新下载页面修改 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
}
})
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Vue如何解决每次发版都要强刷清除浏览器缓存问题
本文链接: https://www.lsjlt.com/news/172419.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0