Skip to content

Commit a135c50

Browse files
ascorbicgatsbybotLB
authored
fix(gatsby-plugin-image): Fix handling of sizes prop in SSR (#28835)
* fix(gatsby-plugin-image): Fix handling of sizes prop in SSR * Update tests * Update tests * Fix types * Update e2e tests * Update e2e-tests/gatsby-static-image/src/pages/constrained.js Co-authored-by: LB <laurie@gatsbyjs.com> * Add test for size override Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com> Co-authored-by: LB <laurie@gatsbyjs.com>
1 parent 930e6b6 commit a135c50

File tree

8 files changed

+77
-31
lines changed

8 files changed

+77
-31
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe(`constrained`, () => {
2+
beforeEach(() => {
3+
cy.visit(`/constrained`).waitForRouteChange()
4+
})
5+
6+
it(`renders a spacer svg`, () => {
7+
cy.getTestElement(`image-constrained`)
8+
.find(`img[role=presentation]`)
9+
.should(`have.attr`, `src`)
10+
.and(`match`, /svg\+xml/)
11+
})
12+
13+
it(`includes sizes attribute with default width`, () => {
14+
cy.getTestElement(`image-constrained`)
15+
.find(`[data-main-image]`)
16+
.should(`have.attr`, `sizes`)
17+
.and(`equal`, `(min-width: 4015px) 4015px, 100vw`)
18+
})
19+
20+
it(`includes sizes attribute with specified width`, () => {
21+
cy.getTestElement(`image-constrained-limit`)
22+
.find(`[data-main-image]`)
23+
.should(`have.attr`, `sizes`)
24+
.and(`equal`, `(min-width: 500px) 500px, 100vw`)
25+
})
26+
27+
it(`overrides sizes`, () => {
28+
cy.getTestElement(`image-constrained-override`)
29+
.find(`[data-main-image]`)
30+
.should(`have.attr`, `sizes`)
31+
.and(`equal`, `100vw`)
32+
})
33+
})

e2e-tests/gatsby-static-image/cypress/integration/fixed.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ describe(`fixed`, () => {
2828
.should(`exist`)
2929
})
3030

31+
it(`includes sizes attribute`, () => {
32+
cy.getTestElement(fixedTestId)
33+
.find(`[data-main-image]`)
34+
.should(`have.attr`, `sizes`)
35+
.should(`equal`, `500px`)
36+
})
3137
})

e2e-tests/gatsby-static-image/cypress/integration/fluid.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ describe(`fluid`, () => {
2323
.should(`have.attr`, `style`)
2424
.and(`match`, /padding/)
2525
})
26+
27+
it(`includes sizes attribute`, () => {
28+
cy.getTestElement(fluidTestId)
29+
.find(`[data-main-image]`)
30+
.should(`have.attr`, `sizes`)
31+
.should(`equal`, `100vw`)
32+
})
2633
})

e2e-tests/gatsby-static-image/src/pages/constrained.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ import { StaticImage } from "gatsby-plugin-image"
33

44
import Layout from "../components/layout"
55

6-
const FluidPage = () => (
6+
const ConstrainedPage = () => (
77
<Layout>
8-
<div data-testid="image-fluid">
8+
<div data-testid="image-constrained">
99
<StaticImage src="../images/citrus-fruits.jpg" alt="Citrus fruits" />
1010
</div>
11-
<div data-testid="image-fluid-png">
12-
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon" />
11+
<div data-testid="image-constrained-limit">
12+
<StaticImage
13+
src="../images/citrus-fruits.jpg"
14+
maxWidth={500}
15+
alt="Citrus fruits"
16+
/>
1317
</div>
14-
<div data-testid="image-fluid-relative">
15-
<StaticImage src="../../content/relative.jpg" alt="Citrus fruits" />
16-
</div>
17-
<div data-testid="invalid-image">
18-
<StaticImage src="./does-not-exist.jpg" />
18+
<div data-testid="image-constrained-override">
19+
<StaticImage
20+
src="../images/citrus-fruits.jpg"
21+
maxWidth={500}
22+
sizes="100vw"
23+
alt="Citrus fruits"
24+
/>
1925
</div>
2026
</Layout>
2127
)
2228

23-
export default FluidPage
29+
export default ConstrainedPage

packages/gatsby-plugin-image/src/components/__tests__/gatsby-image.browser.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { GatsbyImage, ISharpGatsbyImageData } from "../gatsby-image.browser"
2+
import { GatsbyImage, IGatsbyImageData } from "../gatsby-image.browser"
33
import { render, waitFor } from "@testing-library/react"
44
import * as hooks from "../hooks"
55

@@ -14,7 +14,7 @@ jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))
1414

1515
describe(`GatsbyImage browser`, () => {
1616
let beforeHydrationContent: HTMLDivElement
17-
let image: ISharpGatsbyImageData
17+
let image: IGatsbyImageData
1818

1919
beforeEach(() => {
2020
console.warn = jest.fn()
@@ -27,9 +27,9 @@ describe(`GatsbyImage browser`, () => {
2727
width: 100,
2828
height: 100,
2929
layout: `fluid`,
30-
images: { fallback: { src: `some-src-fallback.jpg` } },
30+
images: { fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` } },
3131
placeholder: { sources: [] },
32-
sizes: `192x192`,
32+
3333
backgroundColor: `red`,
3434
}
3535

@@ -168,7 +168,7 @@ describe(`GatsbyImage browser`, () => {
168168
expect(onStartLoadSpy).toBeCalledWith({ wasCached: false })
169169
expect(onLoadSpy).toBeCalled()
170170
expect(hooks.storeImageloaded).toBeCalledWith(
171-
`{"fallback":{"src":"some-src-fallback.jpg"}}`
171+
`{"fallback":{"src":"some-src-fallback.jpg","sizes":"192x192"}}`
172172
)
173173
})
174174

packages/gatsby-plugin-image/src/components/__tests__/gatsby-image.server.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ describe(`GatsbyImage server`, () => {
170170
layout: `constrained`,
171171
images,
172172
placeholder: { sources: [] },
173-
sizes: `192x192`,
174173
backgroundColor: `red`,
175174
}
176175

@@ -186,22 +185,22 @@ describe(`GatsbyImage server`, () => {
186185
data-main-image=""
187186
decoding="async"
188187
loading="lazy"
189-
sizes="192x192"
190188
style="opacity: 0;"
191189
/>
192190
`)
193191
})
194192

195193
it(`has a valid src value when fallback is provided in images`, () => {
196-
const images = { fallback: { src: `some-src-fallback.jpg` } }
194+
const images = {
195+
fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` },
196+
}
197197

198198
const image: IGatsbyImageData = {
199199
width: 100,
200200
height: 100,
201201
layout: `constrained`,
202202
images,
203203
placeholder: { sources: [] },
204-
sizes: `192x192`,
205204
backgroundColor: `red`,
206205
}
207206

@@ -233,6 +232,7 @@ icon64px.png 64w,
233232
icon-retina.png 2x,
234233
icon-ultra.png 3x,
235234
icon.svg`,
235+
sizes: `192x192`,
236236
},
237237
}
238238

@@ -242,7 +242,6 @@ icon.svg`,
242242
layout: `constrained`,
243243
images,
244244
placeholder: { sources: [] },
245-
sizes: `192x192`,
246245
backgroundColor: `red`,
247246
}
248247

@@ -278,7 +277,6 @@ icon.svg`,
278277
layout: `constrained`,
279278
images,
280279
placeholder: { sources: [] },
281-
sizes: `192x192`,
282280
backgroundColor: `red`,
283281
}
284282

@@ -294,7 +292,6 @@ icon.svg`,
294292
data-main-image=""
295293
decoding="async"
296294
loading="lazy"
297-
sizes="192x192"
298295
style="opacity: 0;"
299296
/>
300297
`)
@@ -317,9 +314,11 @@ icon.svg`,
317314
width: 100,
318315
height: 100,
319316
layout: `constrained`,
320-
images: { sources },
317+
images: {
318+
sources,
319+
fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` },
320+
},
321321
placeholder: { sources: [] },
322-
sizes: `192x192`,
323322
backgroundColor: `red`,
324323
}
325324

@@ -339,6 +338,7 @@ icon.svg`,
339338
alt="A fake image for testing purpose"
340339
data-gatsby-image-ssr=""
341340
data-main-image=""
341+
data-src="some-src-fallback.jpg"
342342
decoding="async"
343343
loading="lazy"
344344
sizes="192x192"
@@ -357,7 +357,6 @@ icon.svg`,
357357
layout: `fluid`,
358358
images: {},
359359
placeholder: { sources: [] },
360-
sizes: `192x192`,
361360
backgroundColor: `red`,
362361
}
363362

@@ -383,7 +382,6 @@ icon.svg`,
383382
layout: `fixed`,
384383
images: {},
385384
placeholder: { sources: [] },
386-
sizes: `192x192`,
387385
backgroundColor: `red`,
388386
}
389387

@@ -409,7 +407,6 @@ icon.svg`,
409407
layout: `constrained`,
410408
images: {},
411409
placeholder: { sources: [] },
412-
sizes: `192x192`,
413410
backgroundColor: `red`,
414411
}
415412

packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export interface IGatsbyImageData {
4343
layout: Layout
4444
height?: number
4545
backgroundColor?: string
46-
sizes?: string
4746
images: Pick<MainImageProps, "sources" | "fallback">
4847
placeholder?: Pick<PlaceholderProps, "sources" | "fallback">
4948
width?: number

packages/gatsby-plugin-image/src/components/gatsby-image.server.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
4949
layout,
5050
images,
5151
placeholder,
52-
sizes,
5352
backgroundColor: placeholderBackgroundColor,
5453
} = image
5554

@@ -65,7 +64,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
6564
}
6665
if (images.fallback) {
6766
cleanedImages.fallback = {
68-
src: images.fallback.src,
67+
...images.fallback,
6968
srcSet: images.fallback.srcSet
7069
? removeNewLines(images.fallback.srcSet)
7170
: undefined,
@@ -106,7 +105,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
106105

107106
<MainImage
108107
data-gatsby-image-ssr=""
109-
sizes={sizes}
110108
className={imgClassName}
111109
style={imgStyle}
112110
{...(props as Omit<MainImageProps, "images" | "fallback">)}

0 commit comments

Comments
 (0)