-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
50 lines (44 loc) · 1.11 KB
/
gatsby-node.js
File metadata and controls
50 lines (44 loc) · 1.11 KB
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
const path = require("path");
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const ProductPage = path.resolve("./src/pages/book/_slug.tsx");
const result = await graphql(
`
{
allContentfulProduct {
nodes {
contentful_id
name
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild(
`There was an error loading your Contentful products`,
result.errors
);
return;
}
const products = result.data.allContentfulProduct.nodes;
if (products.length > 0) {
products.forEach((post, index) => {
const previousPostSlug =
index === 0 ? null : products[index - 1].contentful_id;
const nextPostSlug =
index === products.length - 1
? null
: products[index + 1].contentful_id;
createPage({
path: `/book/${post.contentful_id}/`,
component: ProductPage,
context: {
contentful_id: post.contentful_id,
previousPostSlug,
nextPostSlug,
},
});
});
}
};