Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Blog Page if not Found #7

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
58 changes: 30 additions & 28 deletions src/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,38 @@ export async function generateMetadata({
}): Promise<Metadata | undefined> {
let post = await getPost(params.slug);

let {
title,
publishedAt: publishedTime,
summary: description,
image,
} = post.metadata;
let ogImage = image ? `${DATA.url}${image}` : `${DATA.url}/og?title=${title}`;

return {
title,
description,
openGraph: {
if(post.source) {
let {
title,
description,
type: "article",
publishedTime,
url: `${DATA.url}/blog/${post.slug}`,
images: [
{
url: ogImage,
},
],
},
twitter: {
card: "summary_large_image",
publishedAt: publishedTime,
summary: description,
image,
} = post.metadata;
let ogImage = image ? `${DATA.url}${image}` : `${DATA.url}/og?title=${title}`;

return {
title,
description,
images: [ogImage],
},
};
openGraph: {
title,
description,
type: "article",
publishedTime,
url: `${DATA.url}/blog/${post.slug}`,
images: [
{
url: ogImage,
},
],
},
twitter: {
card: "summary_large_image",
title,
description,
images: [ogImage],
},
};
}
}

export default async function Blog({
Expand All @@ -55,7 +57,7 @@ export default async function Blog({
}) {
let post = await getPost(params.slug);

if (!post) {
if (!post.slug) {
notFound();
}

Expand Down
26 changes: 17 additions & 9 deletions src/data/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ export async function markdownToHTML(markdown: string) {
}

export async function getPost(slug: string) {
const filePath = path.join("content", `${slug}.mdx`);
let source = fs.readFileSync(filePath, "utf-8");
const { content: rawContent, data: metadata } = matter(source);
const content = await markdownToHTML(rawContent);
return {
source: content,
metadata,
slug,
};
try {
const filePath = path.join("content", `${slug}.mdx`);
let source = fs.readFileSync(filePath, "utf-8");
const { content: rawContent, data: metadata } = matter(source);
const content = await markdownToHTML(rawContent);
return {
source: content,
metadata,
slug,
};
} catch (error) {
return {
source: null,
metadata: null,
slug: null,
};
}
}

async function getAllPosts(dir: string) {
Expand Down