vue部署后老版本点击报错

js部分==============================================

 

/*
 * @Author: ZGP
 * @Date: 2023-04-19 19:48:23
 * @LastEditors: ZGP
 * @LastEditTime: 2023-04-20 10:22:28
 * @Description:
 * @FilePath: \ynyc_business_control_web\public\utils\MutationObserver.js
 */

(function () {
  // 创建一个MutationObserver实例以观察DOM树变化
  const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => {
      // 只处理子节点添加和移除的变化
      if (mutation.type !== 'childList') {
        return;
      }
      // 获取刚刚添加的节点
      const nodes = Array.from(mutation.addedNodes);
      // 遍历新增节点
      nodes.forEach(node => {
        // 如果节点script类型,则动态请求资源
        if (node.tagName === 'SCRIPT') {
          const url = node.getAttribute('href') || node.getAttribute('src');
          if (url) {
            // 请求资源并检查是否存在
            fetch(url, { method: 'HEAD' }).then(response => {
              if (response.status === 404) {
                alert('检测到有新的版本发布,需要刷新页面以访问最新内容');
                // 刷新页面以重新加载缺失的资源
                window.location.reload();
              }
            });
          }
        }
      });
    });
  });

  // 监视整个文档的变化
  observer.observe(document.documentElement, { childList: true, subtree: true });

  // 在窗口关闭时停止观察并释放资源
  window.addEventListener('beforeunload', () => {
    observer.disconnect();
  });

})();

  

Nginx配置部分==================================================

 

 

posted @ 2023-05-12 15:37  爱喝小果粒  阅读(25)  评论(0)    收藏  举报