-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
75 lines (73 loc) · 2.58 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
const path = require("path");
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const layoutTemplate = path.resolve(`src/templates/pageTemplate.js`);
const postTemplate = path.resolve(`src/templates/postTemplate.js`);
const postListTemplate = path.resolve(`src/templates/postListTemplate.js`);
return graphql(`
query {
allContentfulLayout {
edges {
node {
slug
}
}
}
allContentfulLayoutAllPosts {
edges {
node {
posts {
title
slug
}
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
result.data.allContentfulLayout.edges.forEach((edge) => {
if (edge.node.slug === "404") {
// for 404 page we use custom page at src/pages/404.js
return;
} else if (edge.node.slug === "/") {
createPage({
path: edge.node.slug,
component: layoutTemplate,
context: {
slug: edge.node.slug,
},
});
} else if (edge.node.slug === "post") {
const posts = result.data.allContentfulLayoutAllPosts.edges[0].node.posts;
const postsPerPage = 5;
const numPages = Math.ceil(posts.length / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? edge.node.slug : `${edge.node.slug}/${i + 1}`,
component: postListTemplate,
context: {
slug: "post",
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
});
});
}
});
result.data.allContentfulLayoutAllPosts.edges[0].node.posts.forEach((post) => {
createPage({
path: `/post/${post.slug}`,
component: postTemplate,
context: {
slug: post.slug,
layoutSlug: "post",
},
});
});
});
};