Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Only include posts content where necessary #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export const getStaticPaths: GetStaticPaths = async () => {
};

export const getStaticProps: GetStaticProps = async (ctx) => {
const posts = await getAllPosts();
const posts = await getAllPosts({ withContent: true });
const post = posts.find((post) => post.slug === ctx.params.slug);

return {
Expand Down
3 changes: 2 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const Home: NextPage<{ posts: Post[] }> = ({ posts }) => {
};

export const getStaticProps = async () => {
const posts = await getAllPosts();
// TODO: Generate the feed only when there is a change in the posts
const posts = await getAllPosts({ withContent: true });

if (process.env.NODE_ENV === "production") {
const feed = await generateFeed(posts);
Expand Down
10 changes: 8 additions & 2 deletions src/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const getPostFrontMatter = async (fileName: string) => {
return matter(await fs.readFile(path.join(POSTS_DIR, fileName)));
};

export const getAllPosts = async (): Promise<Array<Post>> => {
export const getAllPosts = async (
{
withContent = false,
}: {
withContent?: boolean;
} = { withContent: false }
): Promise<Array<Post>> => {
let posts = [];
const files = (await fs.readdir(POSTS_DIR)).filter((name) =>
name.endsWith(".mdx")
Expand All @@ -26,7 +32,7 @@ export const getAllPosts = async (): Promise<Array<Post>> => {
title,
slug: slugify(title),
excerpt,
content,
...(withContent && { content }),
fileName,
} as Post);
}
Expand Down