diff --git a/packages/gatsby-plugin-sharp/src/__tests__/process-file.js b/packages/gatsby-plugin-sharp/src/__tests__/process-file.js index e0278ea97f2c6..22e01a9c6b50b 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/process-file.js +++ b/packages/gatsby-plugin-sharp/src/__tests__/process-file.js @@ -1,4 +1,4 @@ -const { createArgsDigest } = require(`../process-file`) +const { createArgsDigest, sortKeys } = require(`../process-file`) describe(`createArgsDigest`, () => { const defaultArgsBaseline = { @@ -94,5 +94,25 @@ describe(`createArgsDigest`, () => { testHashEqual(`maxWidth`, { maxWidth: 500 }) testHashEqual(`base64`, { base64: true }) }) + + describe(`argument sorting`, () => { + it(`sorts nested arguments`, () => { + const args = { + duotone: { + shadow: `#10c5f8`, + highlight: `#32CD32`, + }, + cropFocus: 17, + } + const actual = sortKeys(args) + expect(actual).toEqual({ + cropFocus: 17, + duotone: { + highlight: `#32CD32`, + shadow: `#10c5f8`, + }, + }) + }) + }) }) }) diff --git a/packages/gatsby-plugin-sharp/src/process-file.js b/packages/gatsby-plugin-sharp/src/process-file.js index d7c1e92e70694..659cdcd2cc2a1 100644 --- a/packages/gatsby-plugin-sharp/src/process-file.js +++ b/packages/gatsby-plugin-sharp/src/process-file.js @@ -241,10 +241,29 @@ exports.createArgsDigest = args => { const argsDigest = crypto .createHash(`md5`) - .update(JSON.stringify(filtered, Object.keys(filtered).sort())) + .update(JSON.stringify(sortKeys(filtered))) .digest(`hex`) const argsDigestShort = argsDigest.substr(argsDigest.length - 5) return argsDigestShort } + +const sortKeys = object => { + var sortedObj = {}, + keys = _.keys(object) + + keys = _.sortBy(keys, key => key) + + _.each(keys, key => { + if (typeof object[key] == `object` && !(object[key] instanceof Array)) { + sortedObj[key] = sortKeys(object[key]) + } else { + sortedObj[key] = object[key] + } + }) + + return sortedObj +} + +exports.sortKeys = sortKeys