unis_manager/web/livereload.js

20 lines
751 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 开发模式热加载:轮询 /health 的启动代际(boot),服务端因文件改动重启后自动刷新页面。
// 非 DEV 启动时第一次轮询就自行停止,生产环境零影响。
(function () {
var boot = null;
function poll() {
fetch("/health", { cache: "no-store" })
.then(function (r) { return r.json(); })
.then(function (info) {
if (!info || info.dev !== true) return; // 非开发模式:到此为止,不再轮询
if (boot === null) boot = info.boot;
else if (info.boot !== boot) { location.reload(); return; }
setTimeout(poll, 1000);
})
.catch(function () { setTimeout(poll, 1000); }); /* 服务正在重启,等下一轮 */
}
poll();
})();