-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
113 lines (100 loc) · 2.77 KB
/
gatsby-node.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { CreateNodeArgs, CreatePageArgs, CreatePagesArgs } from "gatsby"
const readingTime = require(`reading-time`)
const path = require(`path`)
const postTemplate = path.resolve(`./src/templates/BlogPost/BlogPost.tsx`)
const photosetTemplate = path.resolve(`./src/templates/Photoset/Photoset.tsx`)
exports.onCreateNode = ({ node, actions, getNode }: CreateNodeArgs) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
// Add reading time estimates to blog posts (Mdx pages)
createNodeField({
node,
name: `timeToRead`,
value: readingTime(node.body),
})
// Add content type to Mdx pages so we know which template/path to use
const parent = getNode(node.parent!)
createNodeField({
node,
name: `contentType`,
value: parent?.sourceInstanceName,
})
}
}
exports.createPages = async ({
graphql,
actions,
reporter,
}: CreatePagesArgs) => {
const { createPage } = actions
/***
* Create pages for mdx nodes based on contentType
***/
const mdxPageQuery: { data?: Queries.MDXPagesQuery; errors?: any } =
await graphql(`
query MDXPages {
allMdx {
nodes {
id
frontmatter {
slug
}
internal {
contentFilePath
}
fields {
contentType
}
}
}
}
`)
if (mdxPageQuery.errors) {
reporter.panicOnBuild("Error loading MDX content", mdxPageQuery.errors)
}
const templates: { [id: string]: string } = {
blog: postTemplate,
photos: photosetTemplate,
}
const mdxPages = mdxPageQuery.data?.allMdx.nodes
mdxPages!.forEach(node => {
createPage({
path: `/${node.fields?.contentType}/${node.frontmatter?.slug}`,
component: `${templates[node.fields!.contentType!]}?__contentFilePath=${
node.internal.contentFilePath
}`,
context: { id: node.id },
})
})
}
exports.onCreatePage = ({ page, actions }: CreatePageArgs) => {
const { createPage } = actions
/***
* Add priority for sitemap
* 1.0 for homepage, all other priorities based on path depth
***/
var priority = 1.0
if (page.path !== "/") {
const re = new RegExp("/", "g")
const depth = page.path.match(re)?.length!
priority = 1.0 - depth * 0.1
}
/***
* Add changefreq for sitemap - weekly by default
* monthly for blog posts, daily for blog index
***/
var changeFreq = "weekly"
if (page.path.includes("/blog/") || page.path.includes("/photos/")) {
changeFreq = "monthly"
} else if (page.path === "/blog" || page.path === "/photos") {
changeFreq = "daily"
}
createPage({
...page,
context: {
...page.context,
priority: priority,
changeFreq: changeFreq,
},
})
}