20 lines
751 B
JavaScript
20 lines
751 B
JavaScript
// 开发模式热加载:轮询 /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();
|
||
})();
|