Skip to content

Commit 9e3de55

Browse files
author
Chris D. Macrae
committed
Try and fix pre-render logic
1 parent abf950a commit 9e3de55

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/pages/articles/[path].tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getAllPosts, getPostBySlug, getPostPaths, getPostSlugs } from "cdm-content";
2+
import { GetStaticPaths, GetStaticProps } from "next";
3+
import { ArticlePage } from "../../views/Article";
4+
5+
export const getStaticPaths: GetStaticPaths = async () => {
6+
const posts = await getAllPosts();
7+
8+
return {
9+
paths: posts.map(p => ({ params: { path: p.slug.replace('/article/', '') }})),
10+
fallback: 'blocking'
11+
}
12+
}
13+
14+
export const getStaticProps: GetStaticProps = async (context) => {
15+
const props: Record<string, any> = {};
16+
const path = Array.isArray(context.params.path) ? context.params.path.join('/') : context.params.path;
17+
const posts = await getAllPosts();
18+
const post = await getPostBySlug(path);
19+
20+
if (post === null) {
21+
return {
22+
notFound: true
23+
}
24+
}
25+
26+
const postIndex = posts?.findIndex(p => p.slug === `/articles/${post.slug}`);
27+
const prev = postIndex > -1 ? posts[postIndex + 1] : null;
28+
const next = postIndex > -1 ? posts[postIndex - 1] : null;
29+
30+
props.post = post;
31+
if (next) props.next = next;
32+
if (prev) props.prev = prev || null;
33+
34+
return {
35+
props
36+
}
37+
}
38+
39+
export default ArticlePage;

0 commit comments

Comments
 (0)