Skip to content

Commit

Permalink
feat: generate static params for posts to improve speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica committed Oct 22, 2024
1 parent b929a25 commit e43742c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/app/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import PostLayout from '@/components/layout/PostLayout';
import { getPostData } from '@/services/content/posts';
import { getAllPosts, getPostData } from '@/services/content/posts';
import { PostData } from '@/types';

interface PostPageProps {
params: Promise<{
slug: string;
}>;
// build static params for all posts
export async function generateStaticParams() {
const posts = await getAllPosts();
return posts.map((post) => ({
slug: post.slug,
}));
}

export default async function PostPage(props: PostPageProps) {
// PostPage component that receives the params directly
export default async function PostPage(props: {
params: Promise<{ slug: string }>;
}) {
const params = await props.params;
const post: PostData = await getPostData(params.slug);

Expand Down
6 changes: 4 additions & 2 deletions src/app/posts/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import PostListLayout from '@/components/layout/PostListLayout';
import { getAllPosts } from '@/services/content/posts';
import { PostData } from '@/types';
import { Suspense } from 'react';

export default async function PostsPage() {
const posts: PostData[] = await getAllPosts();

return (
<div className='container mx-auto p-4'>
<h1 className='mb-6 text-center text-4xl font-bold'>All Posts</h1>

<PostListLayout posts={posts} />
<Suspense fallback={<span>Loading...</span>}>
<PostListLayout posts={posts} />
</Suspense>
</div>
);
}

0 comments on commit e43742c

Please sign in to comment.