Skip to content

Commit c304906

Browse files
authored
fix(examples): Update using-contentful to use gatsby-plugin-image (#29526)
* refactor(createPages): async syntax and await all createPage calls * fix(using-contentful): replace old gatsby-image fragments * refactor(using-contentful): use gatsby-plugin-image instead of gatsby-image * fixup! refactor(using-contentful): use gatsby-plugin-image instead of gatsby-image
1 parent 169eefe commit c304906

File tree

4 files changed

+324
-320
lines changed

4 files changed

+324
-320
lines changed

examples/using-contentful/gatsby-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
title: `Gatsby with Contentful`,
44
},
55
plugins: [
6+
`gatsby-plugin-image`,
67
{
78
resolve: `gatsby-source-contentful`,
89
options: {
Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
const _ = require(`lodash`)
21
const path = require(`path`)
32
const { slash } = require(`gatsby-core-utils`)
43

54
// Implement the Gatsby API “createPages”. This is
65
// called after the Gatsby bootstrap is finished so you have
76
// access to any information necessary to programmatically
87
// create pages.
9-
exports.createPages = ({ graphql, actions }) => {
8+
exports.createPages = async ({ graphql, actions }) => {
109
const { createPage } = actions
1110
// The “graphql” function allows us to run arbitrary
1211
// queries against the local Contentful graphql schema. Think of
1312
// it like the site has a built-in database constructed
1413
// from the fetched data that you can run queries against.
15-
return graphql(
14+
const pageResult = await graphql(
1615
`
1716
{
1817
allContentfulProduct(limit: 1000) {
@@ -25,70 +24,74 @@ exports.createPages = ({ graphql, actions }) => {
2524
}
2625
`
2726
)
28-
.then(result => {
29-
if (result.errors) {
30-
throw result.errors
31-
}
3227

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+
},
5252
})
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
6463
}
6564
}
66-
`
67-
).then(result => {
68-
if (result.errors) {
69-
throw result.errors
7065
}
66+
}
67+
`
68+
)
69+
70+
if (categoryResult.errors) {
71+
throw categoryResult.errors
72+
}
7173

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+
},
9294
})
93-
})
95+
)
96+
)
9497
}

examples/using-contentful/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
"dependencies": {
88
"gatsby": "next",
99
"gatsby-core-utils": "next",
10-
"gatsby-image": "next",
1110
"gatsby-plugin-google-analytics": "next",
11+
"gatsby-plugin-image": "next",
1212
"gatsby-plugin-offline": "next",
1313
"gatsby-plugin-typography": "next",
1414
"gatsby-source-contentful": "next",
1515
"gatsby-transformer-remark": "next",
16-
"lodash": "^4.17.20",
1716
"react": "^16.9.0",
1817
"react-dom": "^16.9.0",
1918
"react-typography": "^0.16.19",
@@ -34,4 +33,4 @@
3433
"build": "gatsby build",
3534
"start": "gatsby serve"
3635
}
37-
}
36+
}

0 commit comments

Comments
 (0)