Skip to content

Commit 1f504eb

Browse files
ENG-437 Remove Error from Blog Build (Closes #71) (#203)
* Refactor generateStaticParams to utilize getFileMetadata for filtering published markdown files in blog and docs pages * Refactor page components in blog and docs to use arrow functions * Update apps/website/app/utils/getFileMetadata.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update apps/website/app/(home)/blog/[slug]/page.tsx Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent ab11952 commit 1f504eb

3 files changed

Lines changed: 90 additions & 22 deletions

File tree

apps/website/app/(docs)/docs/roam/[slug]/page.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { TableOfContents } from "~/components/TableOfContents";
88
import { getProcessedMarkdownFile } from "~/utils/getProcessedMarkdownFile";
99
import { collectSections } from "~/utils/getSections";
1010
import { PrevNextLinks } from "~/components/PrevNextLinks";
11+
import { getFileMetadata } from "~/utils/getFileMetadata";
1112

1213
type Params = {
1314
params: Promise<{
@@ -18,7 +19,7 @@ type Params = {
1819
const PATH = "app/(docs)/docs/roam/pages";
1920
const DIRECTORY = path.join(process.cwd(), PATH);
2021

21-
export default async function Page({ params }: Params) {
22+
const Page = async ({ params }: Params) => {
2223
try {
2324
const { slug } = await params;
2425
const { data, contentHtml } = await getProcessedMarkdownFile({
@@ -45,9 +46,11 @@ export default async function Page({ params }: Params) {
4546
console.error("Error rendering docs page:", error);
4647
return notFound();
4748
}
48-
}
49+
};
50+
51+
export default Page;
4952

50-
export async function generateStaticParams() {
53+
export const generateStaticParams = async () => {
5154
try {
5255
const directoryExists = await fs
5356
.stat(DIRECTORY)
@@ -61,18 +64,32 @@ export async function generateStaticParams() {
6164

6265
const files = await fs.readdir(DIRECTORY);
6366

64-
return files
65-
.filter((filename) => filename.endsWith(".md"))
66-
.map((filename) => ({
67+
const mdFiles = files.filter((filename) => filename.endsWith(".md"));
68+
69+
const publishedFiles = await Promise.all(
70+
mdFiles.map(async (filename) => {
71+
const { published } = await getFileMetadata({
72+
filename,
73+
directory: DIRECTORY,
74+
});
75+
return { filename, published };
76+
}),
77+
);
78+
79+
return publishedFiles
80+
.filter(({ published }) => published)
81+
.map(({ filename }) => ({
6782
slug: filename.replace(/\.md$/, ""),
6883
}));
6984
} catch (error) {
7085
console.error("Error generating static params:", error);
7186
return [];
7287
}
73-
}
88+
};
7489

75-
export async function generateMetadata({ params }: Params): Promise<Metadata> {
90+
export const generateMetadata = async ({
91+
params,
92+
}: Params): Promise<Metadata> => {
7693
try {
7794
const { slug } = await params;
7895
const { data } = await getProcessedMarkdownFile({
@@ -90,4 +107,4 @@ export async function generateMetadata({ params }: Params): Promise<Metadata> {
90107
title: "Docs",
91108
};
92109
}
93-
}
110+
};

apps/website/app/(home)/blog/[slug]/page.tsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { notFound } from "next/navigation";
44
import { Metadata } from "next";
55
import { getProcessedMarkdownFile } from "~/utils/getProcessedMarkdownFile";
66
import { BLOG_PATH } from "~/data/constants";
7+
import { getFileMetadata } from "~/utils/getFileMetadata";
78

89
type Params = {
910
params: Promise<{
1011
slug: string;
1112
}>;
1213
};
1314

14-
export default async function BlogPost({ params }: Params) {
15+
const BlogPost = async ({ params }: Params) => {
1516
try {
1617
const { slug } = await params;
1718
const { data, contentHtml } = await getProcessedMarkdownFile({
@@ -41,41 +42,60 @@ export default async function BlogPost({ params }: Params) {
4142
console.error("Error rendering blog post:", error);
4243
return notFound();
4344
}
44-
}
45+
};
4546

46-
export async function generateStaticParams() {
47+
export const generateStaticParams = async () => {
4748
try {
4849
const blogPath = path.resolve(process.cwd(), BLOG_PATH);
49-
// 1) Check if the directory exists
5050
const directoryExists = await fs
5151
.stat(blogPath)
5252
.then((stats) => stats.isDirectory())
5353
.catch(() => false);
5454

55-
// 2) If it's missing, return empty
5655
if (!directoryExists) {
5756
console.log(
5857
"No app/blog/posts directory found. Returning empty params...",
5958
);
6059
return [];
6160
}
6261

63-
// 3) If it exists, read it
6462
const files = await fs.readdir(blogPath);
6563

66-
// 4) Filter .md files to build the slug array
67-
return files
68-
.filter((filename) => filename.endsWith(".md"))
69-
.map((filename) => ({
64+
const mdFiles = files.filter((filename) => filename.endsWith(".md"));
65+
66+
const results = await Promise.allSettled(
67+
mdFiles.map(async (filename) => {
68+
try {
69+
const { published } = await getFileMetadata({
70+
filename,
71+
directory: BLOG_PATH,
72+
});
73+
return { filename, published };
74+
} catch (error) {
75+
console.error(`Skipping ${filename} due to metadata error:`, error);
76+
return { filename, published: false };
77+
}
78+
}),
79+
);
80+
81+
const publishedFiles = results
82+
.filter((result) => result.status === 'fulfilled')
83+
.map((result) => result.value);
84+
85+
return publishedFiles
86+
.filter(({ published }) => published)
87+
.map(({ filename }) => ({
7088
slug: filename.replace(/\.md$/, ""),
7189
}));
7290
} catch (error) {
7391
console.error("Error generating static params:", error);
7492
return [];
7593
}
76-
}
94+
};
7795

78-
export async function generateMetadata({ params }: Params): Promise<Metadata> {
96+
export const generateMetadata = async ({
97+
params,
98+
}: Params): Promise<Metadata> => {
7999
try {
80100
const { slug } = await params;
81101
const { data } = await getProcessedMarkdownFile({
@@ -93,4 +113,6 @@ export async function generateMetadata({ params }: Params): Promise<Metadata> {
93113
title: "Blog Post",
94114
};
95115
}
96-
}
116+
};
117+
118+
export default BlogPost;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import matter from "gray-matter";
2+
import { PageFrontmatter, PageSchema } from "~/types/schema";
3+
import { getFileContent } from "~/utils/getFileContent";
4+
5+
type Props = {
6+
filename: string;
7+
directory: string;
8+
};
9+
10+
export const getFileMetadata = async ({
11+
filename,
12+
directory,
13+
}: Props): Promise<PageFrontmatter> => {
14+
try {
15+
const fileContent = await getFileContent({
16+
filename,
17+
directory,
18+
});
19+
const { data } = matter(fileContent);
20+
return PageSchema.parse(data);
21+
} catch (error) {
22+
console.error(`Error parsing metadata for ${filename}:`, error);
23+
throw new Error(
24+
`Invalid frontmatter in ${filename}: ${
25+
error instanceof Error ? error.message : 'Unknown error'
26+
}`
27+
);
28+
}
29+
};

0 commit comments

Comments
 (0)