-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
142 lines (132 loc) · 3.71 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/node-apis/
*/
const path = require(`path`)
const { paginate } = require(`gatsby-awesome-pagination`)
/**
* Generate pages
*/
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
// Query all the data
const queryResult = await graphql(`
{
pageQuery: allWpPage {
nodes {
databaseId
uri
}
}
postQuery: allWpPost(sort: { fields: date, order: ASC }) {
edges {
node {
databaseId
uri
}
next {
databaseId
}
previous {
databaseId
}
}
}
catQuery: allWpCategory {
nodes {
databaseId
uri
name
posts {
nodes {
id
}
}
}
}
tagQuery: allWpTag {
nodes {
databaseId
uri
name
posts {
nodes {
id
}
}
}
}
}
`)
if (queryResult.errors) {
reporter.panic("error loading events", queryResult.errors)
return
}
// Generate single page pages
const pages = queryResult.data.pageQuery.nodes
pages.forEach(page => {
createPage({
path: page.uri,
component: path.resolve(`./src/templates/page.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
databaseId: page.databaseId,
},
})
})
// Generate single post pages
const posts = queryResult.data.postQuery.edges
posts.forEach(post => {
createPage({
path: `/posts${post.node.uri}`,
component: path.resolve(`./src/templates/post.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
databaseId: post.node.databaseId,
nextId: post.next ? post.next.databaseId : null,
prevId: post.previous ? post.previous.databaseId : null,
},
})
})
// Create your paginated pages
paginate({
createPage, // The Gatsby `createPage` function
items: posts, // An array of objects
itemsPerPage: 4, // How many items you want per page
pathPrefix: "/posts", // Creates pages like `/blog`, `/blog/2`, etc
component: path.resolve(`./src/templates/posts.js`), // Just like `createPage()`
})
// Create your paginated category indexes
const categories = queryResult.data.catQuery.nodes
categories.map(category => {
paginate({
createPage, // The Gatsby `createPage` function
items: category.posts.nodes, // An array of objects
itemsPerPage: 4, // How many items you want per page
pathPrefix: category.uri, // Creates pages like `/blog`, `/blog/2`, etc
component: path.resolve(`./src/templates/categories.js`), // Just like `createPage()`
context: {
catId: category.databaseId,
catName: category.name,
},
})
})
// Create your paginated tag indexes
const tags = queryResult.data.tagQuery.nodes
tags.map(tag => {
paginate({
createPage, // The Gatsby `createPage` function
items: tag.posts.nodes, // An array of objects
itemsPerPage: 4, // How many items you want per page
pathPrefix: tag.uri, // Creates pages like `/blog`, `/blog/2`, etc
component: path.resolve(`./src/templates/tags.js`), // Just like `createPage()`
context: {
tagId: tag.databaseId,
tagName: tag.name,
},
})
})
}