vue-cli3打包后清除浏览器缓存

tech2025-05-19  9

浏览器缓存原因导致vue 打包后的文件不能即使更新最新代码,缓存里面内容未清除。解决方案:在打包的文件名中添加一个版本号时间戳,以便浏览器能区分

1. 在index.html页面head中添加

<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文件,在打包文件名中添加版本号时间戳

a、创建时间戳

const Timestamp = new Date().getTime();

b、在打包文件名中添加版本号.时间戳

configureWebpack: { output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】 filename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js`, chunkFilename: `[name].${process.env.VUE_APP_Version}.${Timestamp}.js` } },
最新回复(0)