36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
/**
|
|
* 隔离测试:确认 notFound() 返回 200 的根因。
|
|
*
|
|
* 依次访问三种路径,对比状态码:
|
|
* 1. 未匹配任何路由的路径(由 Next.js 自身处理)
|
|
* 2. 匹配动态路由但数据不存在(由页面内 notFound() 处理)
|
|
* 3. 正常存在的数据
|
|
*
|
|
* 用法:node scripts/diagnose-404.mjs [baseUrl]
|
|
*/
|
|
|
|
const BASE = (process.argv[2] ?? "http://localhost:3000").replace(/\/$/, "");
|
|
|
|
const cases = [
|
|
["/zh/this-route-does-not-exist", "未匹配路由"],
|
|
["/zh/posts/zzz-absent", "动态路由 + notFound()"],
|
|
["/zh/posts/building-a-blog-with-nextjs", "正常存在的文章"],
|
|
["/api/posts/zzz-absent", "API 路由 404"],
|
|
];
|
|
|
|
for (const [path, label] of cases) {
|
|
const response = await fetch(`${BASE}${path}`, { redirect: "manual" });
|
|
|
|
// 检查响应头,判断是否为流式响应
|
|
const contentType = response.headers.get("content-type") ?? "";
|
|
|
|
console.log(
|
|
`${String(response.status).padStart(3)} ${label.padEnd(24)} ${path}`,
|
|
);
|
|
console.log(
|
|
` content-type=${contentType.split(";")[0]} ` +
|
|
`x-nextjs-cache=${response.headers.get("x-nextjs-cache") ?? "-"} ` +
|
|
`cache-control=${(response.headers.get("cache-control") ?? "-").slice(0, 40)}`,
|
|
);
|
|
}
|