Vue检测获取最新资源 解决浏览器缓存问题

Vue检测获取最新资源 解决浏览器缓存问题

1、在public文件夹下创建version.json文件

2、vue.config.js中,每次打包动态更新version.json内容

javascript 复制代码
let fs = require('fs');
const version = new Date().getTime();
let vJSON = require('./public/version.json') || {}
if ( process.env.NODE_ENV === "production" ) {
  vJSON = { 'version': version + '' }
  fs.writeFile('./public/version.json', JSON.stringify(vJSON), () => {
    console.log('新版本号生成成功');
  });
}

3、App.vue中使用定时器去检测版本号和本地是否有差异

javascript 复制代码
timer: null // 定时器实例

this.getLatestResources(true)
// 每半小时监测一次是否有更新
this.timer = setInterval(() => {
  this.getLatestResources()
}, 30 * 60 * 1000)

// 获取最新资源
getLatestResources(b = false) {
  this.$axios.get("/xxxxxxx/version.json",{params:{data: new Date().getTime()}}).then(res => {
    let lastVersion = res.data.version
    let storageVersion = localStorage.getItem("version")
    if (!storageVersion) {
      localStorage.setItem("version", lastVersion)
    } else {
      if(storageVersion != lastVersion){
        localStorage.removeItem("version")
        this.timer && clearInterval(this.timer)
        !b && this.$Modal.info({
          title: "提示",
          content: '检测到系统有更新,请刷新浏览器后使用!',
          onOk: () => {
            this.$router.go(0)
          }
        });
        b && this.$router.go(0)
      }
    }
  })
}
相关推荐
一 乐33 分钟前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
啦啦啦_999936 分钟前
Redis-0-业务逻辑
数据库·redis·缓存
科技D人生44 分钟前
Vue.js 学习总结(20)—— Vue-Office 实战:word、pdf、excel、ppt 多种文档的在线预览
vue.js·word·vue-pdf·stylesheet·docx-preview·vue-office
vx1_Biye_Design1 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design1 小时前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
hedley(●'◡'●)1 小时前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
qq5_8115175151 小时前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育1 小时前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再1 小时前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架
hdsoft_huge1 小时前
1panel面板中部署SpringBoot和Vue前后端分离系统 【图文教程】
vue.js·spring boot·后端