Skip to content

Commit 4131a09

Browse files
author
Jason Lengstorf
authored
fix(gatsby-plugin-sharp): ignore sizeByPixelDensity option and add deprecation warning (#13852)
* fix(gatsby-plugin-sharp): ignore sizeByPixelDensity option and add deprecation warning fixes #12743 * docs: add deprecation notice to plugin README * fix: delete tests * Remove pixel density test snapshots
1 parent 2f8f170 commit 4131a09

File tree

4 files changed

+21
-72
lines changed

4 files changed

+21
-72
lines changed

packages/gatsby-plugin-sharp/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ fit strategies `CONTAIN` and `FILL` will not work when `cropFocus` is assigned t
107107
- `maxWidth` (int, default: 800)
108108
- `maxHeight` (int)
109109
- `quality` (int, default: 50)
110-
- `sizeByPixelDensity` (bool, default: false)
111110
- `srcSetBreakpoints` (array of int, default: [])
112111
- `fit` (string, default: '[sharp.fit.cover][6]')
113112
- `background` (string, default: 'rgba(0,0,0,1)')
113+
- [deprecated] `sizeByPixelDensity` (bool, default: false)
114+
- Pixel density is only used in vector images, which Gatsby’s implementation of Sharp doesn’t support. This option is currently a no-op and will be removed in the next major version of Gatsby.
114115

115116
#### Returns
116117

packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap

-36
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,6 @@ Object {
1010
}
1111
`;
1212

13-
exports[`gatsby-plugin-sharp fluid accounts for pixel density 1`] = `
14-
Object {
15-
"aspectRatio": 2.0661764705882355,
16-
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABAklEQVQoz61R7WrCQBDM+7+Cv1qhlIogoUWMoH8KrfgOLVXRKDV4d7kkl6/pbsxpqBIo9mBuZ7N3k51bB7TKsrwZVsexyS2rKerwFhYFVI1DnkMQOHIuCaLB+ayuwXlMaApXgvMoxjTU8MIQE4rMx8SnNffUkU/qM/bbiPAeRfgw5n8tB+QgO1kmUvwROdm0kYHmUNoe+NoU2wZTdVjQH7Isg9YRpFIXor8vpGmKjb/FQQgslit8fi3wvQ/OHXLhxRvDHY7Qd5+x3e2udmNzTUN4fZvhoddH566L+8cn9AZuVcvpLR3e9kEAISWEkDAmbRXkGCcJ1r4PRRNPEkPu9Kn2Azs0CqJYU3JKAAAAAElFTkSuQmCC",
17-
"density": 144,
18-
"originalImg": "/static/1234/e681d/test.png",
19-
"originalName": undefined,
20-
"presentationHeight": 68,
21-
"presentationWidth": 141,
22-
"sizes": "(max-width: 141px) 100vw, 141px",
23-
"src": "/static/1234/e681d/test.png",
24-
"srcSet": "/static/1234/9ec3c/test.png 200w,
25-
/static/1234/e681d/test.png 281w",
26-
"srcSetType": "image/png",
27-
"tracedSVG": undefined,
28-
}
29-
`;
30-
31-
exports[`gatsby-plugin-sharp fluid can optionally ignore pixel density 1`] = `
32-
Object {
33-
"aspectRatio": 2.0661764705882355,
34-
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABAklEQVQoz61R7WrCQBDM+7+Cv1qhlIogoUWMoH8KrfgOLVXRKDV4d7kkl6/pbsxpqBIo9mBuZ7N3k51bB7TKsrwZVsexyS2rKerwFhYFVI1DnkMQOHIuCaLB+ayuwXlMaApXgvMoxjTU8MIQE4rMx8SnNffUkU/qM/bbiPAeRfgw5n8tB+QgO1kmUvwROdm0kYHmUNoe+NoU2wZTdVjQH7Isg9YRpFIXor8vpGmKjb/FQQgslit8fi3wvQ/OHXLhxRvDHY7Qd5+x3e2udmNzTUN4fZvhoddH566L+8cn9AZuVcvpLR3e9kEAISWEkDAmbRXkGCcJ1r4PRRNPEkPu9Kn2Azs0CqJYU3JKAAAAAElFTkSuQmCC",
35-
"density": 144,
36-
"originalImg": "/static/1234/e681d/test.png",
37-
"originalName": undefined,
38-
"presentationHeight": 136,
39-
"presentationWidth": 281,
40-
"sizes": "(max-width: 281px) 100vw, 281px",
41-
"src": "/static/1234/e681d/test.png",
42-
"srcSet": "/static/1234/9ec3c/test.png 200w,
43-
/static/1234/e681d/test.png 281w",
44-
"srcSetType": "image/png",
45-
"tracedSVG": undefined,
46-
}
47-
`;
48-
4913
exports[`gatsby-plugin-sharp fluid includes responsive image properties, e.g. sizes, srcset, etc. 1`] = `
5014
Object {
5115
"aspectRatio": 1,

packages/gatsby-plugin-sharp/src/__tests__/index.js

-22
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,6 @@ describe(`gatsby-plugin-sharp`, () => {
116116
expect(path.parse(result.srcSet).name).toBe(file.name)
117117
})
118118

119-
it(`accounts for pixel density`, async () => {
120-
const result = await fluid({
121-
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
122-
args: {
123-
sizeByPixelDensity: true,
124-
},
125-
})
126-
127-
expect(result).toMatchSnapshot()
128-
})
129-
130-
it(`can optionally ignore pixel density`, async () => {
131-
const result = await fluid({
132-
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
133-
args: {
134-
sizeByPixelDensity: false,
135-
},
136-
})
137-
138-
expect(result).toMatchSnapshot()
139-
})
140-
141119
it(`does not change the arguments object it is given`, async () => {
142120
const args = { maxWidth: 400 }
143121
await fluid({

packages/gatsby-plugin-sharp/src/index.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ async function getTracedSVG(options, file) {
244244

245245
async function fluid({ file, args = {}, reporter, cache }) {
246246
const options = healOptions(getPluginOptions(), args, file.extension)
247+
248+
if (options.sizeByPixelDensity) {
249+
/*
250+
* We learned that `sizeByPixelDensity` is only valid for vector images,
251+
* and Gatsby’s implementation of Sharp doesn’t support vector images.
252+
* This means we should remove this option in the next major version of
253+
* Gatsby, but for now we can no-op and warn.
254+
*
255+
* See https://github.com/gatsbyjs/gatsby/issues/12743
256+
*
257+
* TODO: remove the sizeByPixelDensity option in the next breaking release
258+
*/
259+
reporter.warn(
260+
`the option sizeByPixelDensity is deprecated and should not be used. It will be removed in the next major release of Gatsby.`
261+
)
262+
}
263+
247264
// Account for images with a high pixel density. We assume that these types of
248265
// images are intended to be displayed at their native resolution.
249266
let metadata
@@ -255,11 +272,6 @@ async function fluid({ file, args = {}, reporter, cache }) {
255272
}
256273

257274
const { width, height, density, format } = metadata
258-
const defaultImagePPI = 72 // Standard digital image pixel density
259-
const pixelRatio =
260-
options.sizeByPixelDensity && typeof density === `number` && density > 0
261-
? density / defaultImagePPI
262-
: 1
263275

264276
// if no maxWidth is passed, we need to resize the image based on the passed maxHeight
265277
const fixedDimension =
@@ -275,16 +287,10 @@ async function fluid({ file, args = {}, reporter, cache }) {
275287

276288
let presentationWidth, presentationHeight
277289
if (fixedDimension === `maxWidth`) {
278-
presentationWidth = Math.min(
279-
options.maxWidth,
280-
Math.round(width / pixelRatio)
281-
)
290+
presentationWidth = Math.min(options.maxWidth, width)
282291
presentationHeight = Math.round(presentationWidth * (height / width))
283292
} else {
284-
presentationHeight = Math.min(
285-
options.maxHeight,
286-
Math.round(height / pixelRatio)
287-
)
293+
presentationHeight = Math.min(options.maxHeight, height)
288294
presentationWidth = Math.round(presentationHeight * (width / height))
289295
}
290296

0 commit comments

Comments
 (0)