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

Allow useNextSanityImage to receive a client or a config #62

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

lime
Copy link
Contributor

@lime lime commented Sep 5, 2023

This brings the type in line with @sanity/image-url, which accepts a plain config, or either type of client (from which it then extracts the config).

https://github.com/sanity-io/image-url/blob/v1.0.2/src/builder.ts#L44-L46

I noticed that the type was flip-flopping a bit when upgrading between next-sanity-image 4.0 → 5.0 → 6.0. This should hopefully stabilize it, so that people upgrading can keep passing whichever client argument they already have in place. :)

Related:

This brings the type in line with @sanity/image-url, which accepts either type
of client as well as a plain config.
@lime
Copy link
Contributor Author

lime commented Sep 5, 2023

Also related:

As mentioned in that PR, passing just the config can reduce bundle size, since @sanity/client gets tree-shaken out unless it's used elsewhere.

This PR would make that simpler to write. Instead of

const imageProps = useNextSanityImage({
  config: () => ({
    projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
    dataset: process.env.NEXT_PUBLIC_SANITY_DATASET
  })
}, data.image);

you would just pass

const imageProps = useNextSanityImage({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET
}, data.image);

@martinkz
Copy link

martinkz commented Oct 25, 2023

Will this PR be merged in at some point? I also would like to use next-sanity-image without bundling in Sanity Client.

@joshmeads
Copy link

@martinkz You don't need to, simply return SanityModernClientLike

export const sanityConfig: SanityModernClientLike = {
  config() {
    return {
      projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
      dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
    };
  },
};
const imageProps = useNextSanityImage(sanityConfig, image);

Though I agree with the PR as a whole to allow any style of client config Sanity support.

@lorenzodejong lorenzodejong merged commit f686419 into lorenzodejong:main Nov 13, 2023
@lorenzodejong
Copy link
Owner

Thanks for the PR and sorry for the late reply! I've gotten around testing the changes, and this seems to be in line with the accepted types for the @sanity/image-url. Integrated and will release this in the next version.

Copy link

🎉 This PR is included in version 6.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants