Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Jun 22, 2024
1 parent 067cc06 commit 3c76ced
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/post/post-container.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getPosts from "~/lib/posts/fetch-posts";
import { PostItem } from "./post-item";
import { Post } from "~/lib/types";

export async function PostContainer() {
const posts = await getPosts();
Expand All @@ -14,7 +15,7 @@ export async function PostContainer() {
you can check them out below.
</p>
<div className="flex flex-col space-y-2">
{posts?.map((post) => <PostItem key={post.slug} {...post} />)}
{posts && posts.map((post) => <PostItem key={post!.slug} {...post as Post} />)}
</div>
</div>
);
Expand Down
18 changes: 12 additions & 6 deletions src/lib/posts/fetch-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import matter from "gray-matter";
import fs from "fs/promises";
import path from "path";
import type { Post } from "../types";
import { unstable_cache as cache } from "next/cache";

export const getPosts = cache(async (): Promise<Post[]> => {
export const getPosts = async () => {
const posts = await fs.readdir("./src/posts/");

return Promise.all(
Expand All @@ -21,14 +20,21 @@ export const getPosts = cache(async (): Promise<Post[]> => {
return null;
}

return { ...data, body: content } as Post;
}) as Promise<Post>[],
return {
title: data.title,
slug: data.slug,
date: data.date,
description: data.description,
views: data.views || null,
body: content,
} as Post;
}),
);
});
};

export async function getPost(slug: string) {
const posts = await getPosts();
return posts.find((post) => post!.slug === slug);
return posts.find((post) => post?.slug === slug);
}

export default getPosts;

0 comments on commit 3c76ced

Please sign in to comment.