Skip to content

Commit 6257cdf

Browse files
committed
review changes
1 parent 9895126 commit 6257cdf

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

packages/gatsby-image/src/__tests__/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe(`<Image />`, () => {
175175
expect(console.log).toBeCalled()
176176
})
177177

178-
it(`should warn if multiple sources with no media are used.`, () => {
178+
it(`should warn if image variants provided are missing media keys.`, () => {
179179
jest.spyOn(global.console, `warn`)
180180

181181
render(

packages/gatsby-image/src/index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,26 @@ const convertProps = props => {
3838
}
3939

4040
// convert fluid & fixed to arrays so we only have to work with arrays
41-
convertedProps.fluid = [].concat(convertedProps.fluid).filter(Boolean)
42-
convertedProps.fixed = [].concat(convertedProps.fixed).filter(Boolean)
41+
if (convertedProps.fluid) {
42+
convertedProps.fluid = groupByMedia([].concat(convertedProps.fluid))
43+
}
44+
if (convertedProps.fixed) {
45+
convertedProps.fixed = groupByMedia([].concat(convertedProps.fixed))
46+
}
4347

4448
return convertedProps
4549
}
4650

47-
// Find the source of an image to use as a key in the image cache.
48-
// Use `the first image in either `fixed` or `fluid`
51+
/**
52+
* Find the source of an image to use as a key in the image cache.
53+
* Use `the first image in either `fixed` or `fluid`
54+
* @param {{fluid: {src: string}[], fixed: {src: string}[]}} args
55+
* @return {string}
56+
*/
4957
const getImageSrcKey = ({ fluid, fixed }) => {
50-
const data = (fluid.length && fluid[0]) || (fixed.length && fixed[0])
51-
return data.src
58+
const data = (fluid && fluid[0]) || (fixed && fixed[0])
59+
60+
return data && data.src
5261
}
5362

5463
// Cache if we've seen an image before so we don't bother with
@@ -135,7 +144,7 @@ function groupByMedia(imageVariants) {
135144
console.warn(
136145
`We've found ${
137146
without.length
138-
} sources without a media property. You should only have 1.`
147+
} sources without a media property. They might be ignored by the browser, see: https://www.gatsbyjs.org/packages/gatsby-image/#art-directing-multiple-images`
139148
)
140149
}
141150

@@ -210,12 +219,7 @@ const noscriptImg = props => {
210219
// Earlier versions of gatsby-image during the 2.x cycle did not wrap
211220
// the `Img` component in a `picture` element. This maintains compatibility
212221
// until a breaking change can be introduced in the next major release
213-
const Placeholder = ({
214-
src,
215-
imageVariants,
216-
generateSources,
217-
...spreadProps
218-
}) => {
222+
const Placeholder = ({ src, imageVariants, generateSources, spreadProps }) => {
219223
const baseImage = <Img src={src} {...spreadProps} />
220224

221225
return imageVariants.length > 1 ? (
@@ -406,7 +410,7 @@ class Image extends React.Component {
406410
}
407411

408412
if (fluid.length) {
409-
const imageVariants = groupByMedia(fluid)
413+
const imageVariants = fluid
410414
const image = imageVariants[0]
411415

412416
return (
@@ -449,7 +453,7 @@ class Image extends React.Component {
449453
{image.base64 && (
450454
<Placeholder
451455
src={image.base64}
452-
{...placeholderImageProps}
456+
spreadProps={placeholderImageProps}
453457
imageVariants={imageVariants}
454458
generateSources={generateBase64Sources}
455459
/>
@@ -459,7 +463,7 @@ class Image extends React.Component {
459463
{image.tracedSVG && (
460464
<Placeholder
461465
src={image.tracedSVG}
462-
{...placeholderImageProps}
466+
spreadProps={placeholderImageProps}
463467
imageVariants={imageVariants}
464468
generateSources={generateTracedSVGSources}
465469
/>
@@ -505,7 +509,7 @@ class Image extends React.Component {
505509
}
506510

507511
if (fixed.length) {
508-
const imageVariants = groupByMedia(fixed)
512+
const imageVariants = fixed
509513
const image = imageVariants[0]
510514

511515
const divStyle = {
@@ -546,7 +550,7 @@ class Image extends React.Component {
546550
{image.base64 && (
547551
<Placeholder
548552
src={image.base64}
549-
{...placeholderImageProps}
553+
spreadProps={placeholderImageProps}
550554
imageVariants={imageVariants}
551555
generateSources={generateBase64Sources}
552556
/>
@@ -556,7 +560,7 @@ class Image extends React.Component {
556560
{image.tracedSVG && (
557561
<Placeholder
558562
src={image.tracedSVG}
559-
{...placeholderImageProps}
563+
spreadProps={placeholderImageProps}
560564
imageVariants={imageVariants}
561565
generateSources={generateTracedSVGSources}
562566
/>

0 commit comments

Comments
 (0)