前端独立实现页面是否有发布

1、自动更新js (AutoUpdate.js)

javascript 复制代码
import { Modal } from "antd"

let lastSrcs;
const scriptReg = /\<script.*src=["'](?<src>[^"']+)/gm;
async function extractNewScripts() {
    const html = await fetch('/?_timnestamp=' + Date.now()).then(res => {
        return res.text();
    })
    scriptReg.lastIndex = 0;
    let result = [];
    let match;
    console.log(html)
    while ((match = scriptReg.exec(html))) {
        result.push(match.groups.src)
    }
    return result;
}

async function needUpdate() {
    const newScripts = await extractNewScripts();
    if (!lastSrcs) {
        lastSrcs = newScripts
        return false;
    }
    let result = false;
    if (lastSrcs.length !== newScripts.length) {
        result = true;
    }
    for (let i = 0; i < lastSrcs.length; i++) {
        if (lastSrcs[i] !== newScripts[i]) {
            result = true;
            break;
        }
    }
    lastSrcs = newScripts;
    return result
}

const duration = 5000;
function autoRefresh() {
    setTimeout(async () => {
        const willUpdate = await needUpdate();
        if (willUpdate) {
            Modal.success({
                content: '当前系统有更新,请刷新',
                keyboard: false,
                onOk: () => {
                    location.reload(true);
                }
            });
        }
        autoRefresh();
    }, duration)
};
autoRefresh();

2、修改webpack出口打包文件名称

3、引入AutoUpdate.js

javascript 复制代码
import './util/AutoUpdate';
相关推荐
云和数据.ChenGuang7 小时前
Ascend C 核心技术特性
c语言·开发语言
Hy行者勇哥7 小时前
HTML5 + 原生 CSS + 原生 JS 网页实现攻略
javascript·css·html5
kyle~10 小时前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
小飞侠在吗12 小时前
vue props
前端·javascript·vue.js
NiNi_suanfa13 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
小糖学代码13 小时前
LLM系列:1.python入门:3.布尔型对象
linux·开发语言·python
DsirNg13 小时前
页面栈溢出问题修复总结
前端·微信小程序
小徐_233313 小时前
uni-app 也能远程调试?使用 PageSpy 打开调试的新大门!
前端·微信小程序·uni-app
Data_agent13 小时前
1688获得1688店铺详情API,python请求示例
开发语言·爬虫·python
大怪v13 小时前
【Virtual World 03】上帝之手
前端·javascript