1. The main, js

import '@/utils/version.js'
Copy the code

2.js

/* * @Description: gaobo * @Date: 2021-03-25 15:41:51 * @FilePath: \src\utils\version.js */ import axios from 'axios' import { Dialog } from 'vant'; (function (doc, win) {// timerVersion is a global variable, Default value: null Let timerVersion = setInterval(getHash, 20000) async function getHash() {if(process.env.vue_app_title! = 'development') {/ / the current version of the hash let curScript = document. The getElementsByTagName (' body ') [0]. GetElementsByTagName (' script ') let cur_hash_obj = null for(var item in curScript){ if(curScript[item].src.indexOf("app") ! = -1){ cur_hash_obj = curScript[item] break } } let cur_hash_src = cur_hash_obj.src.split('/') let cur_hash = Cur_hash_src [cur_hash_src.leng-1].split('.')[1] // Request home address in js does not update page const response = await Axios.get (' ${window.location.protocol}//${window.location.host} ') // returns a string, Let el = document.createElement(' HTML ') el.innerhtml = response // Get the hash if(el.getElementsByTagName('script')&&el.getElementsByTagName('script').length){ let newScript = el.getElementsByTagName('script') let new_hash_obj = null for(var item in newScript){ if(newScript[item].src.indexOf("app") ! = -1){ new_hash_obj = newScript[item] break } } let new_hash_src = new_hash_obj.src.split('/') let new_hash = new_hash_src[new_hash_src.length - 1].split('.')[1] if (new_hash ! = cur_hash) {dialog. confirm({title: 'prompt ', message: ` version has been updated, please refresh \ n ${cur_hash} - > ${new_hash} `,}). Then (() = > {window. The location. Reload ()})}}}}}) (window) document,Copy the code

3. The WebPack build script uses hash values

output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[hash].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  },
Copy the code