diff options
| author | Bertrand Yuan <189593334+bertyuan@users.noreply.github.com> | 2026-04-27 15:51:27 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-27 15:51:27 +0800 |
| commit | 658798b3a2378bb6df16cfbb16d707c6fb719e1e (patch) | |
| tree | 63fc32f5f7fda4bbf718f490e3b1640311dbf994 /src/app/(main)/(home)/posts | |
| parent | 8b9c0139a93c8b9d41068e5271d0fc6917d34fab (diff) | |
| parent | 2e7d98420900e1d6749729ea2a94c334e7d65754 (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')
| -rw-r--r-- | src/app/(main)/(home)/posts/[slug]/page.client.tsx | 5 | ||||
| -rw-r--r-- | src/app/(main)/(home)/posts/page.tsx | 39 |
2 files changed, 29 insertions, 15 deletions
diff --git a/src/app/(main)/(home)/posts/[slug]/page.client.tsx b/src/app/(main)/(home)/posts/[slug]/page.client.tsx index 7a97f56..13dbfeb 100644 --- a/src/app/(main)/(home)/posts/[slug]/page.client.tsx +++ b/src/app/(main)/(home)/posts/[slug]/page.client.tsx @@ -7,7 +7,7 @@ import { Icons } from '@/components/icons/icons'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import { Comments } from '@fuma-comment/react'; -import { redirect } from 'next/navigation'; +import { useRouter } from 'next/navigation'; import { useRef } from 'react'; import { toast } from 'sonner'; import { useCopyToClipboard } from 'usehooks-ts'; @@ -42,6 +42,7 @@ export function PostComments({ slug, className, }: { slug: string; className?: string }) { + const router = useRouter(); return ( <Comments page={slug} @@ -49,7 +50,7 @@ export function PostComments({ auth={{ type: 'api', signIn: () => { - redirect('/login'); + router.push('/login'); }, }} /> 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, }, |
