#!/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>/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);