summaryrefslogtreecommitdiff
path: root/src/app/(main)/(home)/posts/page.tsx
diff options
context:
space:
mode:
authorBertrand Yuan <189593334+bertyuan@users.noreply.github.com>2026-04-27 15:51:27 +0800
committerGitHub <noreply@github.com>2026-04-27 15:51:27 +0800
commit658798b3a2378bb6df16cfbb16d707c6fb719e1e (patch)
tree63fc32f5f7fda4bbf718f490e3b1640311dbf994 /src/app/(main)/(home)/posts/page.tsx
parent8b9c0139a93c8b9d41068e5271d0fc6917d34fab (diff)
parent2e7d98420900e1d6749729ea2a94c334e7d65754 (diff)
Merge pull request #19 from bertyuan/copilot/fix-front-backend-connection
fix: patch rough frontend-backend connection issues
Diffstat (limited to 'src/app/(main)/(home)/posts/page.tsx')
-rw-r--r--src/app/(main)/(home)/posts/page.tsx39
1 files changed, 26 insertions, 13 deletions
diff --git a/src/app/(main)/(home)/posts/page.tsx b/src/app/(main)/(home)/posts/page.tsx
index 40ebcda..45016a4 100644
--- a/src/app/(main)/(home)/posts/page.tsx
+++ b/src/app/(main)/(home)/posts/page.tsx
@@ -55,14 +55,18 @@ export default async function Page(props: {
}) {
const searchParams = await props.searchParams;
- const pageIndex = searchParams.page
+ const rawPage = searchParams.page
? Number.parseInt(
Array.isArray(searchParams.page)
- ? searchParams.page[0] ?? ''
+ ? (searchParams.page[0] ?? '')
: searchParams.page,
- 10
- ) - 1
- : 0;
+ 10,
+ )
+ : 1;
+
+ if (Number.isNaN(rawPage)) notFound();
+
+ const pageIndex = rawPage - 1;
// 获取文章(带分页)
const { posts, totalDocs, totalPages } = await getPublishedPosts({
@@ -106,7 +110,9 @@ export default async function Page(props: {
})}
</div>
</Section>
- {totalPages > 1 && <Pagination pageIndex={pageIndex} pageCount={totalPages} />}
+ {totalPages > 1 && (
+ <Pagination pageIndex={pageIndex} pageCount={totalPages} />
+ )}
</>
);
}
@@ -118,21 +124,28 @@ type Props = {
export async function generateMetadata(
props: Props,
- parent: ResolvingMetadata
+ parent: ResolvingMetadata,
): Promise<Metadata> {
const searchParams = await props.searchParams;
- const pageIndex = searchParams.page
- ? Number.parseInt(searchParams.page as string, 10)
+ const rawPage = searchParams.page
+ ? Number.parseInt(
+ Array.isArray(searchParams.page)
+ ? (searchParams.page[0] ?? '')
+ : searchParams.page,
+ 10,
+ )
: 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,
},