-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
166 lines (141 loc) · 5.04 KB
/
gatsby-node.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const path = require(`path`)
const chunk = require(`lodash/chunk`)
// This is a simple debugging tool
// dd() will prettily dump to the terminal and kill the process
// const { dd } = require(`dumper.js`)
/**
* exports.createPages is a built-in Gatsby Node API.
* It's purpose is to allow you to create pages for your site! 💡
*
* See https://www.gatsbyjs.com/docs/node-apis/#createPages for more info.
*/
exports.createPages = async gatsbyUtilities => {
// Query our posts from the GraphQL server
const posts = await getPosts(gatsbyUtilities)
// If there are no posts in WordPress, don't do anything
if (!posts.length) {
return
}
// If there are posts, create pages for them
await createIndividualBlogPostPages({ posts, gatsbyUtilities })
// And a paginated archive
await createBlogPostArchive({ posts, gatsbyUtilities })
}
/**
* This function creates all the individual blog pages in this site
*/
const createIndividualBlogPostPages = async ({ posts, gatsbyUtilities }) =>
Promise.all(
posts.map(({ previous, post, next }) =>
// createPage is an action passed to createPages
// See https://www.gatsbyjs.com/docs/actions#createPage for more info
gatsbyUtilities.actions.createPage({
// Use the WordPress uri as the Gatsby page path
// This is a good idea so that internal links and menus work 👍
path: post.uri,
// use the blog post template as the page component
component: path.resolve(`./src/templates/blog-post.js`),
// `context` is available in the template as a prop and
// as a variable in GraphQL.
context: {
// we need to add the post id here
// so our blog post template knows which blog post
// the current page is (when you open it in a browser)
id: post.id,
// We also use the next and previous id's to query them and add links!
previousPostId: previous ? previous.id : null,
nextPostId: next ? next.id : null,
},
})
)
)
/**
* This function creates all the individual blog pages in this site
*/
async function createBlogPostArchive({ posts, gatsbyUtilities }) {
const graphqlResult = await gatsbyUtilities.graphql(/* GraphQL */ `
{
wp {
readingSettings {
postsPerPage
}
}
}
`)
const { postsPerPage } = graphqlResult.data.wp.readingSettings
const postsChunkedIntoArchivePages = chunk(posts, postsPerPage)
const totalPages = postsChunkedIntoArchivePages.length
return Promise.all(
postsChunkedIntoArchivePages.map(async (_posts, index) => {
const pageNumber = index + 1
const getPagePath = page => {
if (page > 0 && page <= totalPages) {
// Since our homepage is our blog page
// we want the first page to be "/" and any additional pages
// to be numbered.
// "/blog/2" for example
return page === 1 ? `/` : `/blog/${page}`
}
return null
}
// createPage is an action passed to createPages
// See https://www.gatsbyjs.com/docs/actions#createPage for more info
await gatsbyUtilities.actions.createPage({
path: getPagePath(pageNumber),
// use the blog post archive template as the page component
component: path.resolve(`./src/templates/blog-post-archive.js`),
// `context` is available in the template as a prop and
// as a variable in GraphQL.
context: {
// the index of our loop is the offset of which posts we want to display
// so for page 1, 0 * 10 = 0 offset, for page 2, 1 * 10 = 10 posts offset,
// etc
offset: index * postsPerPage,
// We need to tell the template how many posts to display too
postsPerPage,
nextPagePath: getPagePath(pageNumber + 1),
previousPagePath: getPagePath(pageNumber - 1),
},
})
})
)
}
/**
* This function queries Gatsby's GraphQL server and asks for
* All WordPress blog posts. If there are any GraphQL error it throws an error
* Otherwise it will return the posts 🙌
*
* We're passing in the utilities we got from createPages.
* So see https://www.gatsbyjs.com/docs/node-apis/#createPages for more info!
*/
async function getPosts({ graphql, reporter }) {
const graphqlResult = await graphql(/* GraphQL */ `
query WpPosts {
# Query all WordPress blog posts sorted by date
allWpPost(sort: { fields: [date], order: DESC }) {
edges {
previous {
id
}
# note: this is a GraphQL alias. It renames "node" to "post" for this query
# We're doing this because this "node" is a post! It makes our code more readable further down the line.
post: node {
id
uri
}
next {
id
}
}
}
}
`)
if (graphqlResult.errors) {
reporter.panicOnBuild(
`There was an error loading your blog posts`,
graphqlResult.errors
)
return
}
return graphqlResult.data.allWpPost.edges
}