Skip to content

Commit 9690c7c

Browse files
authored
fix(gatsby-plugin-sharp): Handle diff duotone settings (#35075)
1 parent bf8392c commit 9690c7c

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Object {
2626
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAHNUN2pAAAAD1BMVEXugW5lZmbMemybcGl6aWdJY41OAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAAVUlEQVQY062QUQ6AMAhD28L9z2w2RObEH+P7WJquK2TAgEh0SoVfF+GQPk9DC5kvxNLv+QYJ1eBVy0VGxJYJltFfkI/q+87T2mzxQo3pj8rmI6QPGx6CUQCaU8vWagAAAABJRU5ErkJggg==",
2727
"height": 100,
2828
"originalName": "test.png",
29-
"src": "/static/1234/df2e1/test.png",
30-
"srcSet": "/static/1234/df2e1/test.png 1x",
29+
"src": "/static/1234/31820/test.png",
30+
"srcSet": "/static/1234/31820/test.png 1x",
3131
"tracedSVG": undefined,
3232
"width": 100,
3333
}
@@ -38,15 +38,15 @@ Object {
3838
"aspectRatio": 1,
3939
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAMAAAHNUN2pAAAAD1BMVEXugW5lZmbMemybcGl6aWdJY41OAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAAVUlEQVQY062QUQ6AMAhD28L9z2w2RObEH+P7WJquK2TAgEh0SoVfF+GQPk9DC5kvxNLv+QYJ1eBVy0VGxJYJltFfkI/q+87T2mzxQo3pj8rmI6QPGx6CUQCaU8vWagAAAABJRU5ErkJggg==",
4040
"density": 72,
41-
"originalImg": "/static/1234/df2e1/test.png",
41+
"originalImg": "/static/1234/31820/test.png",
4242
"originalName": "test.png",
4343
"presentationHeight": 100,
4444
"presentationWidth": 100,
4545
"sizes": "(max-width: 100px) 100vw, 100px",
46-
"src": "/static/1234/df2e1/test.png",
47-
"srcSet": "/static/1234/bd373/test.png 25w,
48-
/static/1234/3a8be/test.png 50w,
49-
/static/1234/df2e1/test.png 100w",
46+
"src": "/static/1234/31820/test.png",
47+
"srcSet": "/static/1234/c6018/test.png 25w,
48+
/static/1234/c752e/test.png 50w,
49+
/static/1234/31820/test.png 100w",
5050
"srcSetType": "image/png",
5151
"tracedSVG": undefined,
5252
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,28 @@ describe(`gatsby-plugin-sharp`, () => {
615615
const result = await fluid({ file, args })
616616
expect(result).toMatchSnapshot()
617617
})
618+
619+
it(`creates two different images for different duotone settings`, async () => {
620+
const testName = `duotone-digest-test`
621+
const firstImage = await fluid({
622+
file: getFileObject(path.join(__dirname, `images/test.png`), testName),
623+
args: {
624+
maxWidth: 100,
625+
width: 100,
626+
duotone: { highlight: `#BBFFE6`, shadow: `#51758D` },
627+
},
628+
})
629+
const secondImage = await fluid({
630+
file: getFileObject(path.join(__dirname, `images/test.png`), testName),
631+
args: {
632+
maxWidth: 100,
633+
width: 100,
634+
duotone: { highlight: `#F1D283`, shadow: `#000000` },
635+
},
636+
})
637+
638+
expect(firstImage.src).not.toEqual(secondImage.src)
639+
})
618640
})
619641

620642
describe(`stats`, () => {

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ function calculateImageDimensionsAndAspectRatio(file, options) {
8383
}
8484

8585
function prepareQueue({ file, args }) {
86-
const { pathPrefix, ...options } = args
87-
const argsDigestShort = createArgsDigest(options)
88-
const imgSrc = `/${file.name}.${options.toFormat}`
86+
const { pathPrefix, duotone, ...rest } = args
87+
// Duotone is a nested object inside transformOptions and has a [Object: Null Prototype]
88+
// So it's flattened into a new object so that createArgsDigest also takes duotone into account
89+
const digestArgs = Object.assign(rest, duotone)
90+
const argsDigestShort = createArgsDigest(digestArgs)
91+
const imgSrc = `/${file.name}.${args.toFormat}`
8992
const outputDir = path.join(
9093
process.cwd(),
9194
`public`,
@@ -99,11 +102,11 @@ function prepareQueue({ file, args }) {
99102

100103
const { width, height, aspectRatio } = calculateImageDimensionsAndAspectRatio(
101104
file,
102-
options
105+
args
103106
)
104107

105108
// encode the file name for URL
106-
const encodedImgSrc = `/${encodeURIComponent(file.name)}.${options.toFormat}`
109+
const encodedImgSrc = `/${encodeURIComponent(file.name)}.${args.toFormat}`
107110

108111
// Prefix the image src.
109112
const digestDirPrefix = `${file.internal.contentDigest}/${argsDigestShort}`

0 commit comments

Comments
 (0)