From 8d083c6baee8005668ea2c40f61adf9c6345bae5 Mon Sep 17 00:00:00 2001 From: Farzad Yousefzadeh Date: Sat, 5 Feb 2022 22:35:58 +0200 Subject: [PATCH] Only include posts content where necessary --- pages/[slug].tsx | 2 +- pages/index.tsx | 3 ++- src/posts.ts | 10 ++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pages/[slug].tsx b/pages/[slug].tsx index d6bdb145..3909d0fb 100644 --- a/pages/[slug].tsx +++ b/pages/[slug].tsx @@ -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 { diff --git a/pages/index.tsx b/pages/index.tsx index 89fb1e02..a7d26d10 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -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); diff --git a/src/posts.ts b/src/posts.ts index 439f0f18..106580d6 100644 --- a/src/posts.ts +++ b/src/posts.ts @@ -10,7 +10,13 @@ const getPostFrontMatter = async (fileName: string) => { return matter(await fs.readFile(path.join(POSTS_DIR, fileName))); }; -export const getAllPosts = async (): Promise> => { +export const getAllPosts = async ( + { + withContent = false, + }: { + withContent?: boolean; + } = { withContent: false } +): Promise> => { let posts = []; const files = (await fs.readdir(POSTS_DIR)).filter((name) => name.endsWith(".mdx") @@ -26,7 +32,7 @@ export const getAllPosts = async (): Promise> => { title, slug: slugify(title), excerpt, - content, + ...(withContent && { content }), fileName, } as Post); }