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

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';
相关推荐
yong99904 分钟前
MATLAB面波频散曲线反演程序
开发语言·算法·matlab
LeoZY_8 分钟前
开源项目精选:Dear ImGui —— 轻量高效的 C++ 即时模式 GUI 框架
开发语言·c++·ui·开源·开源软件
Fightting889 分钟前
Tkinter Button bind hover message
开发语言·python
会编程的土豆19 分钟前
新手前端小细节
前端·css·html·项目
qq_3363139322 分钟前
javaweb-web基础(springboot入门)
java·开发语言·mysql
摘星编程23 分钟前
OpenHarmony + RN:Text文本书写模式
javascript·react native·react.js
广州华水科技38 分钟前
单北斗GNSS在桥梁形变监测中的应用与技术进展分析
前端
我讲个笑话你可别哭啊39 分钟前
鸿蒙ArkTS快速入门
前端·ts·arkts·鸿蒙·方舟开发框架
CherryLee_121041 分钟前
基于poplar-annotation前端插件封装文本标注组件及使用
前端·文本标注
玄同76544 分钟前
LangChain 1.0 模型接口:多厂商集成与统一调用
开发语言·人工智能·python·langchain·知识图谱·rag·智能体