转自:https://zhuanlan.zhihu.com/p/389290842
原理
将打包后的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
中,配置相关打包配置,代码如下:
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 中,却又需要被打包出来的文件的名称。