-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-node.js
80 lines (72 loc) · 2.19 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
const path = require("path");
/** Create paginated blog post index pages, filtered by topic.
*
* `topic` should be a BlogPostTopic uid, or null for the case where we list all
* posts regardless of topic.
*
* See: https://prismic.io/docs/technologies/paginate-results-gatsby
*/
const createBlogIndexPagesForTopic = async ({ topic, graphql, actions }) => {
const { createPage } = actions;
const POSTS_PER_PAGE = 5;
const topicFilter = topic ? { data: { topic: { uid: { eq: topic } } } } : {};
const queryData = await graphql(
`
query BlogIndexPages($topicFilter: PrismicBlogPostFilterInput) {
allPrismicBlogPost(filter: $topicFilter) {
nodes {
id
}
}
}
`,
{ topicFilter }
);
const posts = queryData.data.allPrismicBlogPost.nodes;
const totalPages = Math.ceil(posts.length / POSTS_PER_PAGE);
const basePageURL = topic ? `/blog/${topic}` : "/blog";
for (let i = 0; i < totalPages; i += 1) {
const url = i === 0 ? basePageURL : `${basePageURL}/${i + 1}`;
createPage({
path: url,
component: path.resolve(
__dirname,
"src/templates/BlogIndexTemplate/blogIndexTemplate.tsx"
),
context: {
limit: POSTS_PER_PAGE,
skip: i * POSTS_PER_PAGE,
currentPage: i,
totalPages,
basePageURL,
filter: topicFilter,
topic,
},
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const blogIndex = createBlogIndexPagesForTopic({
topic: null,
graphql,
actions,
});
const blogPostTopics = await graphql(`
query BlogIndexPages {
allPrismicBlogPostTopic {
nodes {
uid
}
}
}
`);
const blogTopicIndexes =
blogPostTopics.data.allPrismicBlogPostTopic.nodes.map((topic) =>
createBlogIndexPagesForTopic({ topic: topic.uid, graphql, actions })
);
// Even though we don't explicitly do anything with the results of the async
// function calls, this await will cause any exceptions encountered in the
// async calls to be thrown here. This is important in order to avoid silently
// dropping exceptions.
await Promise.all([blogIndex, ...blogTopicIndexes]);
};