Files
novalon-website/scripts/ops/warm-routes.mjs
zhangxiang 702c732d27 fix(contact): 去掉 useSearchParams 让 /contact 可完整预渲染;补发布后 ISR 预热脚本
冷首访白屏的第二个真因:/contact 用 useSearchParams() 读 ?success,客户端组件里
这会让整页在预渲染时退化成 Suspense fallback(首访只看到「加载中…」,限速下 ~6s 才出内容)。
该参数只用于一次性成功提示 ⇒ 改为挂载后从 window.location 读并 replaceState 清理。

- /contact 冷首访 h1 可见:6.0s → 1.5s;HTML 73KB 含正文(此前只有外壳)。
- 新增 scripts/ops/warm-routes.mjs:按 sitemap 预热并断言全 200、第二遍 <1500ms 或 HIT;
  本轮线上第二遍:非 200 = 0、>1500ms = 0。
- 全站冷首访复测:/ 827ms、/about 1652ms、/contact 1511ms、/products 917ms、
  /services/software 922ms、/insights 1532ms。
- runbook:加发布前磁盘检查(≥2G)与「构建失败必须中止」;加 §4.0 预热步骤。
- 事故记录:第三次重建时服务器磁盘 99%,COPY 报 ENOSPC 而 up -d 仍执行(旧镜像被当已发布);
  清理旧备份 1.8G + docker image prune 1.075G 后回到 81%,重建 015b6d75 成功。
2026-10-20 08:23:34 +08:00

43 lines
2.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* 发布后 ISR 预热(2026-10-20,冷首访白屏的第二个真因)。
*
* 现象:/contact 首访 6s 才出现 h1,而 SSR HTML 里本来就有 h1
* ⇒ 不是客户端渲染问题,是**冷 ISR 生成**(缓存 MISS 时同步渲染、首访干等),
* 刷新命中刚生成的缓存所以「刷新就好」。
*
* 用法:node scripts/ops/warm-routes.mjs [--base https://www.novalon.cn] [--concurrency 3]
* 判据:每条路由 200 且最终 x-nextjs-cache 命中(HIT)或 TTFB 明显下降;
* 有任何非 200 退出 1(预热不能"跑完就算成功")。
*/
const arg = (n, d) => { const i = process.argv.indexOf(n); return i > 0 ? process.argv[i + 1] : d; };
const BASE = arg('--base', 'https://www.novalon.cn');
const CONC = Number(arg('--concurrency', '3'));
const sitemap = await fetch(`${BASE}/sitemap.xml`).then((r) => r.text()).catch(() => '');
let routes = [...sitemap.matchAll(/<loc>([^<]+)<\/loc>/g)].map((m) => new URL(m[1]).pathname);
routes = [...new Set(routes.length ? routes : ['/', '/about', '/contact', '/products', '/services', '/solutions', '/insights', '/news', '/cases'])];
async function hit(path) {
const t0 = Date.now();
const r = await fetch(BASE + path, { headers: { 'cache-control': 'no-cache' } }).catch(() => null);
const ms = Date.now() - t0;
return { path, status: r?.status ?? 0, ms, cache: r?.headers.get('x-nextjs-cache') ?? '-' };
}
const first = [];
let i = 0;
await Promise.all(Array.from({ length: CONC }, async () => {
while (i < routes.length) { const p = routes[i++]; first.push(await hit(p)); }
}));
const second = [];
for (const r of routes) second.push(await hit(r));
const fmt = (rows) => rows.map((r) => `${String(r.status)} ${String(r.ms).padStart(5)}ms ${String(r.cache).padEnd(5)} ${r.path}`).join('\n');
console.log(`== 第一遍(触发 ISR 生成)==\n${fmt(first)}`);
console.log(`\n== 第二遍(应命中缓存)==\n${fmt(second)}`);
const bad = [...first, ...second].filter((r) => r.status !== 200);
const slow = second.filter((r) => r.ms > 1500);
console.log(`\n汇总:路由 ${routes.length} · 非 200 ${bad.length} · 第二遍仍 >1500ms ${slow.length}`);
if (bad.length) process.exit(1);