generated from flotiq/gatsby-starter-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gatsby-node.js
56 lines (49 loc) · 1.5 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
const path = require('path');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const singleProduct = path.resolve('./src/templates/product.js');
const result = await graphql(`
query GetProducts {
allProduct(sort: {flotiqInternal: {createdAt: DESC}}) {
edges {
node {
slug
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
const products = result.data.allProduct.edges;
// Create paginated index
const productsPerPage = 12;
const numPages = Math.ceil(products.length / productsPerPage);
Array.from({ length: numPages }).forEach((item, i) => {
createPage({
path: i === 0 ? '/' : `/${i + 1}`,
component: path.resolve('./src/templates/index.js'),
context: {
limit: productsPerPage,
skip: i * productsPerPage,
numPages,
currentPage: i + 1,
},
});
});
// Create events pages.
products.forEach((product, index) => {
const previous = index === products.length - 1 ? null : products[index + 1].node;
const next = index === 0 ? null : products[index - 1].node;
createPage({
path: product.node.slug,
component: singleProduct,
context: {
slug: product.node.slug,
previous,
next,
},
});
});
};