-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
75 lines (70 loc) · 1.45 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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
exports.createPages = async({ graphql, actions }) => {
const { createPage } = actions
const perPage = 5
const pages = await graphql(`
{
blog {
blogPosts(per: ${perPage}) {
pageInfo {
size
total
}
}
}
}
`)
const { size, total } = pages.data.blog.blogPosts.pageInfo
const pageCount = Math.max(total, 1)
for (let i = 1; i <= pageCount; i += 1) {
const pageQuery = await graphql(`
{
blog {
blogPosts(per: ${perPage}, page: ${i}) {
nodes {
title
kicker
featuredImage
body {
html
}
preview {
html
}
slug
meta {
published
}
}
}
}
}
`)
createPage({
path: i === 1 ? '/blog/' : `/blog/page/${i}/`,
component: path.resolve('./src/components/Blog/Page.tsx'),
context: {
data: pageQuery.data,
currentPage: i,
perPage: size,
prevPagePath: i <= 2 ? '/blog/' : `/blog/page/${i - 1}/`,
nextPagePath: `/blog/page/${i + 1}/`,
hasPrevPage: i !== 1,
hasNextPage: i < total,
},
})
const { nodes } = pageQuery.data.blog.blogPosts
nodes.forEach((node, i) => {
const next = nodes[i + 1] ? nodes[i + 1] : nodes[0]
createPage({
path: `/blog/${node.slug}/`,
component: path.resolve('./src/components/Blog/Post.tsx'),
context: {
node,
next,
},
})
})
}
}