1
- const _ = require ( `lodash` )
2
1
const path = require ( `path` )
3
2
const { slash } = require ( `gatsby-core-utils` )
4
3
5
4
// Implement the Gatsby API “createPages”. This is
6
5
// called after the Gatsby bootstrap is finished so you have
7
6
// access to any information necessary to programmatically
8
7
// create pages.
9
- exports . createPages = ( { graphql, actions } ) => {
8
+ exports . createPages = async ( { graphql, actions } ) => {
10
9
const { createPage } = actions
11
10
// The “graphql” function allows us to run arbitrary
12
11
// queries against the local Contentful graphql schema. Think of
13
12
// it like the site has a built-in database constructed
14
13
// from the fetched data that you can run queries against.
15
- return graphql (
14
+ const pageResult = await graphql (
16
15
`
17
16
{
18
17
allContentfulProduct(limit: 1000) {
@@ -25,70 +24,74 @@ exports.createPages = ({ graphql, actions }) => {
25
24
}
26
25
`
27
26
)
28
- . then ( result => {
29
- if ( result . errors ) {
30
- throw result . errors
31
- }
32
27
33
- // Create Product pages
34
- const productTemplate = path . resolve ( `./src/templates/product.js` )
35
- // We want to create a detailed page for each
36
- // product node. We'll just use the Contentful id for the slug.
37
- _ . each ( result . data . allContentfulProduct . edges , edge => {
38
- // Gatsby uses Redux to manage its internal state.
39
- // Plugins and sites can use functions like "createPage"
40
- // to interact with Gatsby.
41
- createPage ( {
42
- // Each page is required to have a `path` as well
43
- // as a template component. The `context` is
44
- // optional but is often necessary so the template
45
- // can query data specific to each page.
46
- path : `/products/${ edge . node . id } /` ,
47
- component : slash ( productTemplate ) ,
48
- context : {
49
- id : edge . node . id ,
50
- } ,
51
- } )
28
+ if ( pageResult . errors ) {
29
+ throw pageResult . errors
30
+ }
31
+
32
+ // Create Product pages
33
+ const productTemplate = path . resolve ( `./src/templates/product.js` )
34
+
35
+ // We want to create a detailed page for each
36
+ // product node. We'll just use the Contentful id for the slug.
37
+ await Promise . all (
38
+ pageResult . data . allContentfulProduct . edges . map ( edge =>
39
+ // Gatsby uses Redux to manage its internal state.
40
+ // Plugins and sites can use functions like "createPage"
41
+ // to interact with Gatsby.
42
+ createPage ( {
43
+ // Each page is required to have a `path` as well
44
+ // as a template component. The `context` is
45
+ // optional but is often necessary so the template
46
+ // can query data specific to each page.
47
+ path : `/products/${ edge . node . id } /` ,
48
+ component : slash ( productTemplate ) ,
49
+ context : {
50
+ id : edge . node . id ,
51
+ } ,
52
52
} )
53
- } )
54
- . then ( ( ) => {
55
- graphql (
56
- `
57
- {
58
- allContentfulCategory(limit: 1000) {
59
- edges {
60
- node {
61
- id
62
- }
63
- }
53
+ )
54
+ )
55
+
56
+ const categoryResult = await graphql (
57
+ `
58
+ {
59
+ allContentfulCategory(limit: 1000) {
60
+ edges {
61
+ node {
62
+ id
64
63
}
65
64
}
66
- `
67
- ) . then ( result => {
68
- if ( result . errors ) {
69
- throw result . errors
70
65
}
66
+ }
67
+ `
68
+ )
69
+
70
+ if ( categoryResult . errors ) {
71
+ throw categoryResult . errors
72
+ }
71
73
72
- // Create Category pages
73
- const categoryTemplate = path . resolve ( `./src/templates/category.js` )
74
- // We want to create a detailed page for each
75
- // category node. We'll just use the Contentful id for the slug.
76
- _ . each ( result . data . allContentfulCategory . edges , edge => {
77
- // Gatsby uses Redux to manage its internal state.
78
- // Plugins and sites can use functions like "createPage"
79
- // to interact with Gatsby .
80
- createPage ( {
81
- // Each page is required to have a `path` as well
82
- // as a template component. The `context` is
83
- // optional but is often necessary so the template
84
- // can query data specific to each page.
85
- path : `/categories/ ${ edge . node . id } /` ,
86
- component : slash ( categoryTemplate ) ,
87
- context : {
88
- id : edge . node . id ,
89
- } ,
90
- } )
91
- } )
74
+ // Create Category pages
75
+ const categoryTemplate = path . resolve ( `./src/templates/category.js` )
76
+
77
+ // We want to create a detailed page for each
78
+ // category node. We'll just use the Contentful id for the slug.
79
+ return Promise . all (
80
+ categoryResult . data . allContentfulCategory . edges . map ( edge =>
81
+ // Gatsby uses Redux to manage its internal state .
82
+ // Plugins and sites can use functions like " createPage"
83
+ // to interact with Gatsby.
84
+ createPage ( {
85
+ // Each page is required to have a `path` as well
86
+ // as a template component. The `context` is
87
+ // optional but is often necessary so the template
88
+ // can query data specific to each page.
89
+ path : `/categories/ ${ edge . node . id } /` ,
90
+ component : slash ( categoryTemplate ) ,
91
+ context : {
92
+ id : edge . node . id ,
93
+ } ,
92
94
} )
93
- } )
95
+ )
96
+ )
94
97
}
0 commit comments