-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
142 lines (133 loc) · 3.61 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
const config = require('./src/utils/siteConfig')
const path = require(`path`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const loadPosts = new Promise((resolve, reject) => {
graphql(`
{
allContentfulPost(
sort: { fields: [publishDate], order: DESC }
limit: 10000
) {
edges {
node {
slug
publishDate
}
}
}
}
`).then(result => {
const posts = result.data.allContentfulPost.edges
const postsPerFirstPage = config.postsPerHomePage
const postsPerPage = config.postsPerPage
const numPages = Math.ceil(
posts.slice(postsPerFirstPage).length / postsPerPage
)
// Create main home page
createPage({
path: `/`,
component: path.resolve(`./src/templates/index.jsx`),
context: {
limit: postsPerFirstPage,
skip: 0,
numPages: numPages + 1,
currentPage: 1,
},
})
// Create additional pagination on home page if needed
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: `/${i + 2}/`,
component: path.resolve(`./src/templates/index.jsx`),
context: {
limit: postsPerPage,
skip: i * postsPerPage + postsPerFirstPage,
numPages: numPages + 1,
currentPage: i + 2,
},
})
})
// Create each individual post
posts.forEach((edge, i) => {
const prev = i === 0 ? null : posts[i - 1].node
const next = i === posts.length - 1 ? null : posts[i + 1].node
createPage({
path: `${edge.node.slug}/`,
component: path.resolve(`./src/templates/post.jsx`),
context: {
slug: edge.node.slug,
prev,
next,
},
})
})
resolve()
})
})
const loadTags = new Promise((resolve, reject) => {
graphql(`
{
allContentfulTag {
edges {
node {
slug
post {
id
}
}
}
}
}
`).then(result => {
const tags = result.data.allContentfulTag.edges
const postsPerPage = config.postsPerPage
// Create tag pages with pagination if needed
tags.map(({ node }) => {
const totalPosts = node.post !== null ? node.post.length : 0
const numPages = Math.ceil(totalPosts / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path:
i === 0 ? `/tag/${node.slug}/` : `/tag/${node.slug}/${i + 1}/`,
component: path.resolve(`./src/templates/tag.jsx`),
context: {
slug: node.slug,
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numPages,
currentPage: i + 1,
},
})
})
})
resolve()
})
})
const loadPages = new Promise((resolve, reject) => {
graphql(`
{
allContentfulPage {
edges {
node {
slug
}
}
}
}
`).then(result => {
const pages = result.data.allContentfulPage.edges
pages.map(({ node }) => {
createPage({
path: `${node.slug}/`,
component: path.resolve(`./src/templates/page.jsx`),
context: {
slug: node.slug,
},
})
})
resolve()
})
})
return Promise.all([loadPosts, loadTags, loadPages])
}