-
Notifications
You must be signed in to change notification settings - Fork 1
/
[...slug].tsx
68 lines (55 loc) · 1.64 KB
/
[...slug].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from "react";
import PostCategorySlug from "components/Post/PostCategorySlug";
import { makeMeta } from "utils/makeMeta";
import handleStructuredData, { StructuredDataType } from "data/metadata";
import { checkCategory } from "utils/checkCategory";
import * as Articles from "contentlayer/generated";
import type { GetStaticPropsContext } from "next";
import type { Post } from "types/posts";
import type { DocumentTypes } from ".contentlayer/generated/types";
interface Props {
post: Post;
structuredData: StructuredDataType;
}
const Slug = (props: Props) => {
return <PostCategorySlug {...props} />;
};
export const getStaticPaths = async () => {
const paths = Articles.allDocuments.map((p: Post) => ({
params: { slug: [p._raw.sourceFileDir, p.slug] },
}));
return {
paths,
fallback: false,
};
};
type PageParams = {
slug: string[];
};
export const getStaticProps = async (
context: GetStaticPropsContext<PageParams>
) => {
if (!context.params) {
return;
}
const { slug } = context.params;
const category = slug[0];
let post: Post | undefined;
let structuredData: StructuredDataType | undefined;
const getArticle = (curDos: DocumentTypes[]) => {
post = curDos.find((p: Post) => p.slug === slug[1]);
if (post) {
const customMeta = makeMeta(post);
structuredData = handleStructuredData(customMeta);
return;
}
};
checkCategory(category, getArticle);
return {
props: {
post: post ? post : null,
structuredData: structuredData ? structuredData : null,
},
};
};
export default Slug;