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

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';
相关推荐
luojiaao1 分钟前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码2 分钟前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人4 分钟前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
狼性书生6 分钟前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
书语时9 分钟前
ES6 Promise 状态机
前端·javascript·es6
英英_11 分钟前
视频爬虫的Python库
开发语言·python·音视频
猛犸MAMMOTH16 分钟前
Python打卡第46天
开发语言·python·机器学习
拉不动的猪37 分钟前
管理不同权限用户的左侧菜单展示以及权限按钮的启用 / 禁用之其中一种解决方案
前端·javascript·面试
多多*42 分钟前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
梓仁沐白42 分钟前
【Kotlin】协程
开发语言·python·kotlin