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-sharp): Handle diff duotone settings #35075

Merged
merged 5 commits into from
Mar 14, 2022
Merged

Conversation

LekoArts
Copy link
Contributor

@LekoArts LekoArts commented Mar 8, 2022

Description

The createArgsDigest(options) receives an options with the following input:

options {
  key: 'value',
  duotone: [Object: null prototype] { highlight: '#F1D283', shadow: '#000000' }
}

The node-object-hash doesn't recognize the [Object: null prototype] as an object an gives it the unknown type. So in the hashing then the contents of duotone don't make a difference.

We probably didn't catch this yet as while transformOptions also has the same null prototype (Googling pointed me towards GraphQL), we spread it: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-sharp/src/image-data.ts#L220

Since duotone is the only key that is an object, the one-off destructuring for duotone is fine IMO

Related Issues

Fixes #35054

@LekoArts LekoArts added the topic: media Related to gatsby-plugin-image, or general image/media processing topics label Mar 8, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Mar 8, 2022
@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Mar 8, 2022
@LekoArts LekoArts marked this pull request as ready for review March 10, 2022 12:05
@wardpeet
Copy link
Contributor

Should we also try to fix it in the upstream dependency because there might be other cases we're not aware of

wardpeet
wardpeet previously approved these changes Mar 10, 2022
@LekoArts
Copy link
Contributor Author

LekoArts commented Mar 10, 2022

Should we also try to fix it in the upstream dependency because there might be other cases we're not aware of

A "fix" for it would be JSON.parse(JSON.stringify(stuff)) and I'm not sure if they'd accept that. I've looked through our options and duotone is the only one that is an object, the rest is string, number etc so no null protoype

@imjoshin
Copy link
Contributor

Should we also try to fix it in the upstream dependency because there might be other cases we're not aware of

A "fix" for it would be JSON.parse(JSON.stringify(stuff)) and I'm not sure if they'd accept that

Here's a working change at https://github.com/SkeLLLa/node-object-hash/blob/master/src/objectSorter.ts#L208:

var objectName = typeof obj.toString === 'function' ? obj.toString() : 'unknown';

if (objectName === 'unknown') {
    try {
        var json = JSON.stringify(obj)
        if (json) {
            objectName = json
        }
    } catch {
        // pass
    }
}

I think it may be worth asking so we don't get bit by this again. I could also see it as an opt-in flag on their config.

Comment on lines 86 to 87
const { pathPrefix, duotone, ...rest } = args
const options = Object.assign(rest, duotone)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct ? It seems to me like it would "flatten" duotone:

// some example
const args = { foo: `bar`, duotone: { wat: true }, a: 5 }
const { pathPrefix, duotone, ...rest } = args
const TEST = Object.assign(rest, duotone)
// TEST = {foo: 'bar', a: 5, wat: true}

Copy link
Contributor Author

@LekoArts LekoArts Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options is only used for the digest, args is used for options return so should be fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right - then my suggestions would be to rename options to something else just to indicate it's not really "options" anymore as shape is different (just to avoid confusion as I had above if someone read it some time later). Ideally with some comment with context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pieh Done

@pieh
Copy link
Contributor

pieh commented Mar 10, 2022

I wonder if instead we should adjust hasher to handle null prototype? So that we don't have to handle cases like that in the future in other plugins. To me that would be more reliable fix (tho might be much more involved?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug found when trying to generate two versions of the same image in a graphql query
4 participants