From cd124571ccdf818403842cfbb00d70517700a224 Mon Sep 17 00:00:00 2001 From: DefectingCat Date: Fri, 19 May 2023 09:16:05 +0800 Subject: [PATCH] add static path generate --- app/blog/[page]/page.tsx | 8 ++++++-- app/p/[slug]/page.tsx | 10 +++++++--- lib/posts.ts | 14 ++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/blog/[page]/page.tsx b/app/blog/[page]/page.tsx index bb2e908..40ae233 100644 --- a/app/blog/[page]/page.tsx +++ b/app/blog/[page]/page.tsx @@ -1,10 +1,14 @@ import PostCard from 'app/blog/post-card'; import PostCardLoading from 'app/blog/post-card-loading'; import Pagination from 'components/rua/rua-pagination'; -import { postLists, PostPerPage } from 'lib/posts'; +import { getPostListPath, postLists, PostPerPage } from 'lib/posts'; import { notFound } from 'next/navigation'; import { Fragment, Suspense } from 'react'; +export async function generateStaticParams() { + return await getPostListPath(); +} + export default async function Page({ params }: { params: { page: string } }) { const page = Number(params.page); if (!page) notFound(); @@ -36,4 +40,4 @@ export default async function Page({ params }: { params: { page: string } }) { /> ); -} +} \ No newline at end of file diff --git a/app/p/[slug]/page.tsx b/app/p/[slug]/page.tsx index 7f8ac32..e1459ab 100644 --- a/app/p/[slug]/page.tsx +++ b/app/p/[slug]/page.tsx @@ -2,20 +2,24 @@ import rehypePrism from '@mapbox/rehype-prism'; import components from 'components/mdx/components'; import PostToc from 'components/post/post-toc'; import data from 'content/mdx-data'; -import { readSinglePost } from 'lib/posts'; +import { readSinglePost, allPostsPath } from 'lib/posts'; import { SingleToc, generateToc } from 'lib/utils'; import { compileMDX } from 'next-mdx-remote/rsc'; +import dynamic from 'next/dynamic'; import { notFound } from 'next/navigation'; import rehypeSlug from 'rehype-slug'; import remarkGfm from 'remark-gfm'; import { Post } from 'types'; -import dynamic from 'next/dynamic'; const PostCommnetLine = dynamic( () => import('components/post/post-commnet-line') ); const PostComment = dynamic(() => import('components/post/post-comment')); +export async function generateStaticParams() { + return await allPostsPath(); +} + const Page = async ({ params, }: { @@ -70,4 +74,4 @@ const Page = async ({ ); }; -export default Page; +export default Page; \ No newline at end of file diff --git a/lib/posts.ts b/lib/posts.ts index 03f261a..5f90a93 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -37,18 +37,14 @@ export const postLists = async (): Promise => { * Get posts list page. */ export const PostPerPage = 10; -export type PostPath = { params: { page: string } }; +export type PostPath = { page: string }; const postPathCallback = (prev: PostPath[], _: unknown, i: number) => - i + 1 > 2 - ? prev.concat({ - params: { page: (i + 1).toString() }, - }) - : prev; + i + 1 > 2 ? prev.concat({ page: (i + 1).toString() }) : prev; export const getPostListPath = async () => { const length = (await fs.readdir(path.join(dataPath))).length; const pages = Math.ceil(length / PostPerPage); return Array.from({ length: pages }).reduce(postPathCallback, [ - { params: { page: '2' } }, + { page: '2' }, ]); }; @@ -60,9 +56,7 @@ export const getPostListPath = async () => { const readFilePath = async (filename: string) => { const slug = filename.replace(/\.mdx$/, ''); return { - params: { - slug, - }, + slug, }; };