vue项目打包后浏览器缓存问题及解决

vue项目打包后浏览器缓存

1、第一步需要在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中添加如下代码:

const path = require("path"); // 获取当前的时间戳
let timeStamp = new Date().getTime();
module.exports = {
 filenameHashing: false, // 打包的时候不使用hash值.因为我们有时间戳来确定项目的唯一性了.
 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`,
 }
 }
};

可在自己的配置文件中相对的写入以上代码

3、需要在nginx中配置不去缓存index.html文件内容(nginx.conf中配置)

if ($request_filename ~* ^.*?.(html|htm)$) {
 add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
 }

vue打包更新后缓存

需要服务器配置

nginx: 里面cache-control:no-cache,no-store

总结

作者:可可爱i原文地址:https://blog.csdn.net/qq_53441451/article/details/124423258

%s 个评论

要回复文章请先登录注册