Skip to content

Commit ccdc61d

Browse files
lsirivongKyleAMathews
authored andcommitted
[gatsby-source-contentful] handle assets without files for improved preview api support (#3004)
* Handle Contentful Assets with no file * Update image api queries to handle null file
1 parent 9519584 commit ccdc61d

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

packages/gatsby-source-contentful/src/__tests__/__snapshots__/normalize.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,7 @@ Set {
16201620
"wtrHxeu3zEoEce2MokCSi",
16211621
"c10TkaLheGeQG6qQGqWYqUI",
16221622
"c6s3iG2OVmoUcosmA8ocqsG",
1623+
"FAKE_ID_1phEMTm908mCCeYuSqCkyK",
16231624
}
16241625
`;
16251626

@@ -2197,6 +2198,36 @@ Array [
21972198
"title": "House icon",
21982199
},
21992200
],
2201+
Array [
2202+
Object {
2203+
"children": Array [],
2204+
"description": "",
2205+
"file": null,
2206+
"id": "FAKE_ID_1phEMTm908mCCeYuSqCkyK",
2207+
"internal": Object {
2208+
"contentDigest": "9cd202ba8ae3dd79494cd56c1ec453bc",
2209+
"type": "ContentfulAsset",
2210+
},
2211+
"node_locale": "en-US",
2212+
"parent": null,
2213+
"title": "",
2214+
},
2215+
],
2216+
Array [
2217+
Object {
2218+
"children": Array [],
2219+
"description": "",
2220+
"file": null,
2221+
"id": "FAKE_ID_1phEMTm908mCCeYuSqCkyK___de",
2222+
"internal": Object {
2223+
"contentDigest": "b9803a73e3cb3df628b452fb0aa56e88",
2224+
"type": "ContentfulAsset",
2225+
},
2226+
"node_locale": "de",
2227+
"parent": null,
2228+
"title": "",
2229+
},
2230+
],
22002231
]
22012232
`;
22022233

packages/gatsby-source-contentful/src/__tests__/data.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,24 @@
18661866
}
18671867
}
18681868
}
1869+
},
1870+
{
1871+
"sys": {
1872+
"space": {
1873+
"sys": {
1874+
"type": "Link",
1875+
"linkType": "Space",
1876+
"id": "rocybtov1ozk"
1877+
}
1878+
},
1879+
"type": "Asset",
1880+
"id": "FAKE_ID_1phEMTm908mCCeYuSqCkyK",
1881+
"revision": 0,
1882+
"createdAt": "2017-11-16T20:47:42.248Z",
1883+
"updatedAt": "2017-11-16T20:47:42.248Z",
1884+
"locale": "en-US"
1885+
},
1886+
"fields": {}
18691887
}
18701888
],
18711889
"deletedEntries": [],

packages/gatsby-source-contentful/src/__tests__/extend-node-type.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ describe(`contentful extend node type`, () => {
3434
},
3535
},
3636
}
37+
38+
const nullFileImage = {
39+
defaultLocale: `en-US`,
40+
file: null,
41+
}
42+
3743
describe(`resolveResponsiveResolution`, () => {
3844
it(`generates responsive resolution data for images`, async () => {
3945
const resp = await resolveResponsiveResolution(image, { width: 400 })
@@ -72,6 +78,10 @@ describe(`contentful extend node type`, () => {
7278
expect(resp.width).toBe(450)
7379
expect(resp.height).toBe(399)
7480
})
81+
it(`handles null`, async () => {
82+
const resp = await resolveResponsiveResolution(nullFileImage, { width: 400 })
83+
expect(resp).toBe(null)
84+
})
7585
})
7686
describe(`resolveResponsiveSizes`, () => {
7787
it(`generates responsive size data for images`, async () => {
@@ -88,6 +98,10 @@ describe(`contentful extend node type`, () => {
8898
expect(resp.srcSet.length).toBeGreaterThan(1)
8999
expect(resp).toMatchSnapshot()
90100
})
101+
it(`handles null`, async () => {
102+
const resp = await resolveResponsiveSizes(nullFileImage, { maxWidth: 400 })
103+
expect(resp).toBe(null)
104+
})
91105
})
92106
describe(`resolveResize`, () => {
93107
it(`generates resized images`, async () => {
@@ -102,5 +116,9 @@ describe(`contentful extend node type`, () => {
102116
})
103117
expect(resp).toMatchSnapshot()
104118
})
119+
it(`handles null`, async () => {
120+
const resp = await resolveResize(nullFileImage, { width: 400 })
121+
expect(resp).toBe(null)
122+
})
105123
})
106124
})

packages/gatsby-source-contentful/src/extend-node-type.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const ImageCropFocusType = new GraphQLEnumType({
6969
const isImage = image =>
7070
_.includes(
7171
[`image/jpeg`, `image/jpg`, `image/png`, `image/webp`, `image/gif`],
72-
image.file.contentType
72+
_.get(image, `file.contentType`)
7373
)
7474

7575
const getBase64Image = (imgUrl, args = {}) => {
@@ -214,6 +214,7 @@ exports.resolveResponsiveResolution = resolveResponsiveResolution
214214

215215
const resolveResponsiveSizes = (image, options) => {
216216
if (!isImage(image)) return null
217+
217218
return new Promise(resolve => {
218219
getBase64ImageAndBasicMeasurements(image, options).then(
219220
({ contentType, base64Str, width, height, aspectRatio }) => {
@@ -285,13 +286,10 @@ const resolveResponsiveSizes = (image, options) => {
285286
}
286287
exports.resolveResponsiveSizes = resolveResponsiveSizes
287288

288-
const resolveResize = (image, options) =>
289-
new Promise(resolve => {
290-
if (!isImage(image)) {
291-
resolve()
292-
return
293-
}
289+
const resolveResize = (image, options) => {
290+
if (!isImage(image)) return null
294291

292+
return new Promise(resolve => {
295293
getBase64ImageAndBasicMeasurements(image, options).then(
296294
({ contentType, base64Str, width, height, aspectRatio }) => {
297295
// If the user selected a height (so cropping) and fit option
@@ -324,6 +322,7 @@ const resolveResize = (image, options) =>
324322
}
325323
)
326324
})
325+
}
327326

328327
exports.resolveResize = resolveResize
329328

packages/gatsby-source-contentful/src/normalize.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ exports.createAssetNodes = ({
354354
//
355355
// Get localized fields.
356356
localizedAsset.fields = {
357-
file: getField(localizedAsset.fields.file),
357+
file: localizedAsset.fields.file
358+
? getField(localizedAsset.fields.file)
359+
: null,
358360
title: localizedAsset.fields.title
359361
? getField(localizedAsset.fields.title)
360362
: ``,

0 commit comments

Comments
 (0)