This commit is contained in:
1 parent
c1541b80df
commit
b4c7908029
97 files changed
+12403
-154
No files matched your search
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 检查「不存在的文章 / 草稿」是否返回真正的 HTTP 404。
|
||||
*
|
||||
* 背景:在动态路由(无 generateStaticParams 预生成)中,
|
||||
* `notFound()` 的行为依赖渲染模式。若页面被当作静态/缓存内容渲染,
|
||||
* 可能只渲染 404 内容但状态码仍是 200——这对 SEO 有害,
|
||||
* 搜索引擎会把不存在的 URL 当成有效页面收录。
|
||||
*
|
||||
* 用法:node scripts/check-404.mjs [baseUrl]
|
||||
*/
|
||||
|
||||
const BASE = (process.argv[2] ?? "http://localhost:3000").replace(/\/$/, "");
|
||||
|
||||
const cases = [
|
||||
{ path: "/zh/posts/definitely-not-exist", expect: 404, label: "不存在的文章" },
|
||||
{ path: "/zh/posts/web-performance-priority", expect: 404, label: "草稿文章" },
|
||||
{ path: "/zh/no-such-page", expect: 404, label: "不存在的页面" },
|
||||
{ path: "/zh/posts/building-a-blog-with-nextjs", expect: 200, label: "已发布文章" },
|
||||
{ path: "/api/posts/definitely-not-exist", expect: 404, label: "API:不存在的文章" },
|
||||
{ path: "/api/posts/web-performance-priority", expect: 404, label: "API:草稿" },
|
||||
];
|
||||
|
||||
let pass = 0;
|
||||
let fail = 0;
|
||||
|
||||
for (const testCase of cases) {
|
||||
const response = await fetch(`${BASE}${testCase.path}`, { redirect: "manual" });
|
||||
const okStatus = response.status === testCase.expect;
|
||||
|
||||
if (okStatus) {
|
||||
pass++;
|
||||
console.log(` [PASS] ${testCase.label}: ${response.status}`);
|
||||
} else {
|
||||
fail++;
|
||||
console.log(
|
||||
` [FAIL] ${testCase.label}: 期望 ${testCase.expect},实际 ${response.status}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n 通过 ${pass} / ${pass + fail}`);
|
||||
process.exitCode = fail > 0 ? 1 : 0;
|
||||
Reference in new issue
Block a user