Skip to content

Commit

Permalink
Throw err in case of same url for blog
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjapotocin committed Oct 28, 2023
1 parent 8e3656e commit ccc6b28
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pages/blog/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,24 @@ const Text = styled.p<{ $colorPurple?: boolean; $capitalize?: boolean }>`
`;

export async function getStaticPaths() {
const paths = getBlogsMetadata().map((post: any) => ({
const paths: {
params: { slug: string };
}[] = getBlogsMetadata().map((post: any) => ({
params: { slug: post.url },
}));

var slugsSet = new Set();

paths.forEach(({ params: { slug } }) => {
if (slugsSet.has(slug)) {
throw new Error(
`Looks like there are two blogs with the same title or url: ${slug}`,
);
}

slugsSet.add(slug);
});

return {
paths,
fallback: false,
Expand All @@ -88,7 +102,7 @@ export async function getStaticProps({
params: { slug: string };
}) {
const { fullFilePath, publicFilePath, thumbImage } = getBlogsMetadata().find(
(post: any) => post.url === slug
(post: any) => post.url === slug,
);
const mdFile = fs.readFileSync(fullFilePath, "utf-8");
const { data: frontmatter, content } = matter(mdFile);
Expand All @@ -100,7 +114,7 @@ export async function getStaticProps({
remarkPlugins: [remarkGfm, remarkUnwrapImages], // Add remarkGfm to support MD tables
rehypePlugins: [rehypeHighlight as any, getImageData], // Adds webp src, width and height to images
},
}
},
);

return {
Expand Down

0 comments on commit ccc6b28

Please sign in to comment.