Update public-api.js via upload script - 2026-10-21 10:49:40
This commit is contained in:
parent
6180070534
commit
adcdaeda55
|
|
@ -450,15 +450,35 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
|||
res.sendFile(path.resolve(uploadsDir, SITE_ASSETS_DIR, file), (err) => { if (err) res.status(404).end(); });
|
||||
});
|
||||
|
||||
// 后台"发布到网站"按钮:触发 Cloudflare Pages 重新构建(需要后台登录)
|
||||
// 后台"更新到网站"按钮:触发 Cloudflare Pages 重新构建(需要后台登录)
|
||||
// Cloudflare 在上一次构建还没跑完时会返回 304(不新建构建)。这时不让用户等:
|
||||
// 服务器记下"还有一次待更新",每 20 秒自动重试,直到 Cloudflare 接受新的构建为止。
|
||||
// 期间不管点多少次(不同产品),都合并成这一次待更新——它开始构建时会拉取最新的全部数据。
|
||||
const deployQ = { pending: false, timer: null, tries: 0, since: 0 };
|
||||
const hook = async () => (await fetch(conf('CF_DEPLOY_HOOK'), { method: 'POST' })).status;
|
||||
function scheduleRetry() {
|
||||
if (deployQ.timer) return;
|
||||
deployQ.timer = setTimeout(async () => {
|
||||
deployQ.timer = null;
|
||||
deployQ.tries++;
|
||||
let st = 0;
|
||||
try { st = await hook(); } catch (e) { st = 0; }
|
||||
if (st >= 200 && st < 300) { deployQ.pending = false; deployQ.tries = 0; console.log('[public-api] 排队的网站更新已提交给 Cloudflare'); return; }
|
||||
if (deployQ.tries >= 45) { deployQ.pending = false; deployQ.tries = 0; console.warn('[public-api] 网站更新重试 15 分钟仍未被接受,已放弃(最后状态 ' + st + ')'); return; }
|
||||
scheduleRetry();
|
||||
}, 20000);
|
||||
}
|
||||
app.post('/api/site/deploy', requireAuth, async (req, res) => {
|
||||
if (!conf('CF_DEPLOY_HOOK')) return res.status(500).json({ error: '未配置 CF_DEPLOY_HOOK' });
|
||||
if (deployQ.pending) return res.json({ ok: true, queued: true, note: '已加入更新队列:当前这次构建结束后自动再更新一次,包含你刚才的修改,不用再点' });
|
||||
try {
|
||||
const r = await fetch(conf('CF_DEPLOY_HOOK'), { method: 'POST' });
|
||||
// 304:Cloudflare 没有新建构建任务——通常是刚刚已经触发过、上一次构建还在排队或进行中
|
||||
// (短时间内连点、或者刚推送过代码)。不算失败,等那一次构建跑完就行。
|
||||
if (r.status === 304) return res.json({ ok: true, note: '已有一次更新在进行中,稍等1~2分钟即可生效;如果之后网站还没变化,再点一次' });
|
||||
if (!r.ok) throw new Error(`Cloudflare 返回 HTTP ${r.status}`);
|
||||
const st = await hook();
|
||||
if (st === 304) {
|
||||
deployQ.pending = true; deployQ.tries = 0; deployQ.since = Date.now();
|
||||
scheduleRetry();
|
||||
return res.json({ ok: true, queued: true, note: '已加入更新队列:当前这次构建结束后自动再更新一次,包含你刚才的修改,不用再点' });
|
||||
}
|
||||
if (st < 200 || st >= 300) throw new Error(`Cloudflare 返回 HTTP ${st}`);
|
||||
res.json({ ok: true });
|
||||
} catch (e) { res.status(502).json({ error: e.message }); }
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue