Skip to content

Commit ae226b5

Browse files
Bi11wardpeetLekoArts
authored
fix(gatsby-plugin-image): Add outputPixelDensities to SHARP_ATTRIBUTES (#36203)
Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: LekoArts <[email protected]>
1 parent d5113c1 commit ae226b5

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

e2e-tests/visual-regression/cypress/integration/image.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,19 @@ describe(`StaticImage`, () => {
4848
})
4949
})
5050
})
51+
describe(`sharp props`, () => {
52+
it(`respects outputPixelDensities on ${size.join("x")}`, () => {
53+
cy.viewport(...size)
54+
cy.visit("/static-images/sharp-props")
55+
// Wait for main image to load
56+
cy.get("[data-main-image]").should("exist")
57+
// Check srcset for 1x, 2x, 3x
58+
cy.get("[data-main-image]").then(img => {
59+
expect(img[0].attributes.srcset.value).to.contain(`300w`)
60+
expect(img[0].attributes.srcset.value).to.contain(`600w`)
61+
expect(img[0].attributes.srcset.value).to.contain(`900w`)
62+
})
63+
})
64+
})
5165
})
5266
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from "react"
2+
import { StaticImage } from "gatsby-plugin-image"
3+
import { TestWrapper } from "../../components/test-wrapper"
4+
import Layout from "../../components/layout"
5+
6+
const Page = () => {
7+
return (
8+
<Layout>
9+
<h1>Defining sharp props</h1>
10+
<TestWrapper>
11+
<StaticImage
12+
src="../../images/cornwall.jpg"
13+
loading="eager"
14+
width={300}
15+
alt="cornwall"
16+
layout="fixed"
17+
outputPixelDensities={[1, 2, 3]}
18+
/>
19+
</TestWrapper>
20+
</Layout>
21+
)
22+
}
23+
24+
export default Page

packages/gatsby-plugin-image/src/babel-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const SHARP_ATTRIBUTES = new Set([
2323
`sizes`,
2424
`backgroundColor`,
2525
`breakpoints`,
26+
`outputPixelDensities`,
2627
])
2728

2829
export function normalizeProps(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export function _getStaticImage(
4141
webpOptions,
4242
avifOptions,
4343
blurredOptions,
44+
breakpoints,
45+
outputPixelDensities,
4446
/* eslint-enable @typescript-eslint/no-unused-vars */
4547
...props
4648
}): ReactElement {

packages/gatsby-plugin-image/src/image-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface ISharpGatsbyImageArgs {
5050
avifOptions?: Record<string, unknown>
5151
blurredOptions?: { width?: number; toFormat?: ImageFormat }
5252
breakpoints?: Array<number>
53+
outputPixelDensities?: Array<number>
5354
backgroundColor?: string
5455
}
5556

0 commit comments

Comments
 (0)