From 2e7d98420900e1d6749729ea2a94c334e7d65754 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 27 Apr 2026 07:12:51 +0000 Subject: fix: guard generateMetadata against NaN and sub-1 page numbers Agent-Logs-Url: https://github.com/bertyuan/next-blog/sessions/a183ee36-4a5d-49b3-a43d-dba5985217dd Co-authored-by: bertyuan <189593334+bertyuan@users.noreply.github.com> --- src/app/(main)/(home)/posts/page.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/app/(main)/(home)/posts/page.tsx') diff --git a/src/app/(main)/(home)/posts/page.tsx b/src/app/(main)/(home)/posts/page.tsx index 376f5c1..45016a4 100644 --- a/src/app/(main)/(home)/posts/page.tsx +++ b/src/app/(main)/(home)/posts/page.tsx @@ -128,7 +128,7 @@ export async function generateMetadata( ): Promise { const searchParams = await props.searchParams; - const pageIndex = searchParams.page + const rawPage = searchParams.page ? Number.parseInt( Array.isArray(searchParams.page) ? (searchParams.page[0] ?? '') @@ -137,13 +137,15 @@ export async function generateMetadata( ) : 1; - const isFirstPage = pageIndex === 1 || !searchParams.page; - const pageTitle = isFirstPage ? 'Posts' : `Posts - Page ${pageIndex}`; - const canonicalUrl = isFirstPage ? '/posts' : `/posts?page=${pageIndex}`; + if (Number.isNaN(rawPage) || rawPage < 1) notFound(); + + const isFirstPage = rawPage === 1 || !searchParams.page; + const pageTitle = isFirstPage ? 'Posts' : `Posts - Page ${rawPage}`; + const canonicalUrl = isFirstPage ? '/posts' : `/posts?page=${rawPage}`; return createMetadata({ title: pageTitle, - description: `Posts${!isFirstPage ? ` - Page ${pageIndex}` : ''}`, + description: `Posts${!isFirstPage ? ` - Page ${rawPage}` : ''}`, openGraph: { url: canonicalUrl, }, -- cgit v1.2.3