graysonhicks / gatsby-plugin-remote-images

:floppy_disk: A Gatsby.js plugin for downloading and linking remote images from another node's field for the benefits of gatsby-image.
155 stars 39 forks source link

Adding an option to skip undefined urls #134

Closed ryan-talus closed 1 year ago

ryan-talus commented 1 year ago

Just found this plugin today, it's extremely helpful!

I hit this same issue: https://github.com/graysonhicks/gatsby-plugin-remote-images/issues/120

I'm using this plugin with another which creates a structure with a lot of null values so I've added a quick, minimal-changes fix.

This adds an option, skipUndefinedUrls, which short-circuits node creation where either the url passed to createImageNode() is falsy or the url returned from prepareUrl() (when defined) is falsy. This skips undefined urls and adds an easy way for the user to implement their own "undefined" values by returning undefined from the prepareUrl() function.

Example of using this change to set options to skip both null urls and urls which are 'N/A':

  plugins: [
    {
      resolve: `gatsby-plugin-remote-images`,
      options: {
        nodeType: 'MyRemoteNode',
        imagePath: 'data.imageUrl',
        // ** ALL OPTIONAL BELOW HERE: **
        skipUndefinedUrls: true,
        prepareUrl: (url: string) => {
          if (!url || url === 'N/A') return undefined
          return url
        },
      },
    },
  ]

Default behavior (without setting skipUndefinedUrls: true is unchanged so that the currently implemented errors aren't affected.

graysonhicks commented 1 year ago

Thanks @ryan-talus I'll test this out and merge it in!

ryan-talus commented 1 year ago

Thanks—let me know if I should change anything in the PR!

colinrim commented 1 year ago

Hi @graysonhicks Any update on this issue? Planed release to include this feature? Thanks

graysonhicks commented 1 year ago

Merged and released with 3.6.2, thanks!

graysonhicks commented 1 year ago

Oops, the changes on this PR were on the wrong gatsby-node file. Fixed and published as 3.6.3