Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-image): Fix handling of sizes prop in SSR (#28835) #28867

Merged
merged 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/constrained.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe(`constrained`, () => {
beforeEach(() => {
cy.visit(`/constrained`).waitForRouteChange()
})

it(`renders a spacer svg`, () => {
cy.getTestElement(`image-constrained`)
.find(`img[role=presentation]`)
.should(`have.attr`, `src`)
.and(`match`, /svg\+xml/)
})

it(`includes sizes attribute with default width`, () => {
cy.getTestElement(`image-constrained`)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.and(`equal`, `(min-width: 4015px) 4015px, 100vw`)
})

it(`includes sizes attribute with specified width`, () => {
cy.getTestElement(`image-constrained-limit`)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.and(`equal`, `(min-width: 500px) 500px, 100vw`)
})

it(`overrides sizes`, () => {
cy.getTestElement(`image-constrained-override`)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.and(`equal`, `100vw`)
})
})
6 changes: 6 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/fixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ describe(`fixed`, () => {
.should(`exist`)
})

it(`includes sizes attribute`, () => {
cy.getTestElement(fixedTestId)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.should(`equal`, `500px`)
})
})
7 changes: 7 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/fluid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ describe(`fluid`, () => {
.should(`have.attr`, `style`)
.and(`match`, /padding/)
})

it(`includes sizes attribute`, () => {
cy.getTestElement(fluidTestId)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.should(`equal`, `100vw`)
})
})
26 changes: 16 additions & 10 deletions e2e-tests/gatsby-static-image/src/pages/constrained.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ import { StaticImage } from "gatsby-plugin-image"

import Layout from "../components/layout"

const FluidPage = () => (
const ConstrainedPage = () => (
<Layout>
<div data-testid="image-fluid">
<div data-testid="image-constrained">
<StaticImage src="../images/citrus-fruits.jpg" alt="Citrus fruits" />
</div>
<div data-testid="image-fluid-png">
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon" />
<div data-testid="image-constrained-limit">
<StaticImage
src="../images/citrus-fruits.jpg"
maxWidth={500}
alt="Citrus fruits"
/>
</div>
<div data-testid="image-fluid-relative">
<StaticImage src="../../content/relative.jpg" alt="Citrus fruits" />
</div>
<div data-testid="invalid-image">
<StaticImage src="./does-not-exist.jpg" />
<div data-testid="image-constrained-override">
<StaticImage
src="../images/citrus-fruits.jpg"
maxWidth={500}
sizes="100vw"
alt="Citrus fruits"
/>
</div>
</Layout>
)

export default FluidPage
export default ConstrainedPage
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { GatsbyImage, ISharpGatsbyImageData } from "../gatsby-image.browser"
import { GatsbyImage, IGatsbyImageData } from "../gatsby-image.browser"
import { render, waitFor } from "@testing-library/react"
import * as hooks from "../hooks"

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

describe(`GatsbyImage browser`, () => {
let beforeHydrationContent: HTMLDivElement
let image: ISharpGatsbyImageData
let image: IGatsbyImageData

beforeEach(() => {
console.warn = jest.fn()
Expand All @@ -27,9 +27,9 @@ describe(`GatsbyImage browser`, () => {
width: 100,
height: 100,
layout: `fluid`,
images: { fallback: { src: `some-src-fallback.jpg` } },
images: { fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` } },
placeholder: { sources: [] },
sizes: `192x192`,

backgroundColor: `red`,
}

Expand Down Expand Up @@ -168,7 +168,7 @@ describe(`GatsbyImage browser`, () => {
expect(onStartLoadSpy).toBeCalledWith({ wasCached: false })
expect(onLoadSpy).toBeCalled()
expect(hooks.storeImageloaded).toBeCalledWith(
`{"fallback":{"src":"some-src-fallback.jpg"}}`
`{"fallback":{"src":"some-src-fallback.jpg","sizes":"192x192"}}`
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ describe(`GatsbyImage server`, () => {
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -186,22 +185,22 @@ describe(`GatsbyImage server`, () => {
data-main-image=""
decoding="async"
loading="lazy"
sizes="192x192"
style="opacity: 0;"
/>
`)
})

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

const image: IGatsbyImageData = {
width: 100,
height: 100,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down Expand Up @@ -233,6 +232,7 @@ icon64px.png 64w,
icon-retina.png 2x,
icon-ultra.png 3x,
icon.svg`,
sizes: `192x192`,
},
}

Expand All @@ -242,7 +242,6 @@ icon.svg`,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down Expand Up @@ -278,7 +277,6 @@ icon.svg`,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -294,7 +292,6 @@ icon.svg`,
data-main-image=""
decoding="async"
loading="lazy"
sizes="192x192"
style="opacity: 0;"
/>
`)
Expand All @@ -317,9 +314,11 @@ icon.svg`,
width: 100,
height: 100,
layout: `constrained`,
images: { sources },
images: {
sources,
fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` },
},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -339,6 +338,7 @@ icon.svg`,
alt="A fake image for testing purpose"
data-gatsby-image-ssr=""
data-main-image=""
data-src="some-src-fallback.jpg"
decoding="async"
loading="lazy"
sizes="192x192"
Expand All @@ -357,7 +357,6 @@ icon.svg`,
layout: `fluid`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -383,7 +382,6 @@ icon.svg`,
layout: `fixed`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -409,7 +407,6 @@ icon.svg`,
layout: `constrained`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export interface IGatsbyImageData {
layout: Layout
height?: number
backgroundColor?: string
sizes?: string
images: Pick<MainImageProps, "sources" | "fallback">
placeholder?: Pick<PlaceholderProps, "sources" | "fallback">
width?: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
layout,
images,
placeholder,
sizes,
backgroundColor: placeholderBackgroundColor,
} = image

Expand All @@ -65,7 +64,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
}
if (images.fallback) {
cleanedImages.fallback = {
src: images.fallback.src,
...images.fallback,
srcSet: images.fallback.srcSet
? removeNewLines(images.fallback.srcSet)
: undefined,
Expand Down Expand Up @@ -106,7 +105,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI

<MainImage
data-gatsby-image-ssr=""
sizes={sizes}
className={imgClassName}
style={imgStyle}
{...(props as Omit<MainImageProps, "images" | "fallback">)}
Expand Down