vue 版本更新使用webpack优化打包,解决用户浏览器缓存问题

1、修改 webpack.prod.conf.js 文件

const version = new Date().getTime();
output: {
	path: config.build.assetsRoot,
	filename: utils.assetsPath('js/[name].[chunkhash:8].' + version + '.js'),
	chunkFilename: utils.assetsPath('js/[name].[chunkhash:8].' + version + '.js')
}

优化版:

const date = new Date();
const version = moment(date).format('YYYYMMDDHHmmssSSS'); // 打包时候的版本号
const timestamp = date.getTime(); // 时间戳
output: {
	path: config.build.assetsRoot,
	filename: utils.assetsPath(`js/[name].[chunkhash:8].${ version }.js?_t=${ timestamp }`),
	chunkFilename: utils.assetsPath(`js/[name].[chunkhash:8].${ version }.js?_t=${ timestamp }`)
}

优化后的效果:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
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、html 页面加载脚本的时候给脚本后面加一个时间戳,修改 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
	}
})

vue-cli 里的默认配置,css 和 js 的名字都加了哈希值,所以新版本 css、js 和就旧版本的名字是不同的,不会有缓存问题。但是 index.html 放到服务器里去的时候,index.html 在服务器端可能是有缓存的,这需要在服务器配置不让缓存 index.html。

4、nginx 配置,让 index.html 不缓存。

location = /index.html {
    add_header Cache-Control "no-cache, no-store";
}
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值