Skip to content

Conversation

@isakgustavsen
Copy link
Collaborator

@isakgustavsen isakgustavsen commented Jul 19, 2025

πŸ”— Linked issue

Resolves #162
Resolves #400

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This enhances the SanityImage component by integrating it with @nuxt/image.

When the @nuxt/image module is installed, it is automatically used for rendering, enabling image optimizations. If the module is not installed, the component gracefully falls back to using a standard <img> tag.

- Introduced `isNuxtImageEnabled` option in Sanity module options to track if @nuxt/image is enabled.
- Updated the Sanity public runtime config to include the new option.
- Enhanced the `sanity-image` component to conditionally render using NuxtImg when @nuxt/image is enabled.
@netlify
Copy link

netlify bot commented Jul 19, 2025

βœ… Deploy Preview for nuxt-sanity-module ready!

Name Link
πŸ”¨ Latest commit 0f7835a
πŸ” Latest deploy log https://app.netlify.com/projects/nuxt-sanity-module/deploys/68e8fdfcd58b7b00086e1a7c
😎 Deploy Preview https://deploy-preview-1255--nuxt-sanity-module.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@isakgustavsen
Copy link
Collaborator Author

Im having an issue where the playground doesn't like import('#compnents') so I can't test the functionality to fail silently, but it works without it. It can be removed but would nicer to keep it. Provider has to be set to Sanity in the config for Nuxt Image but I'm sure there is a way hard code it into the sanity image component that I couldn't figure out

@isakgustavsen
Copy link
Collaborator Author

awaiting updating the docs for this until the new docs get merged

@rylanharper
Copy link

@isakgustavsen Dude Isak, you're the man!

@isakgustavsen
Copy link
Collaborator Author

Unsure if this is a good way to handle this @danielroe but come with feedback and i'll update as needed

@danielroe
Copy link
Collaborator

might be worth checking how it's done here:

https://github.com/nuxt-modules/mdc/blob/a69242835d10733cfa8a9149dd74c47ea75b92df/src/module.ts#L181-L189

(I wouldn't do it exactly this way but we can probably do it at build time rather than adding it to the runtime component.)

@isakgustavsen isakgustavsen marked this pull request as ready for review July 23, 2025 10:49
@isakgustavsen isakgustavsen requested a review from danielroe as a code owner July 23, 2025 10:49
Copy link
Collaborator

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

What if we also, in <SanityContent>, automatically add <NuxtImg> as a custom component with the sanity provider to render images in portable text? And we can probably also configure the sanity provider in Nuxt image automatically so the user doesn't have to do so separately.

what do you think?

@isakgustavsen
Copy link
Collaborator Author

I think it would be a welcome improvement,. Should it be added only if Nuxt Image is installed or should be do it with a standard image tag too?

On the point of adding as a provider I couldn't get that working, but it would be good

@rylanharper
Copy link

Random side note, when Nuxt Image v2 releases, I plan on using both the new Shopify provider and Sanity provider together. This is a good idea, but there are some minor use-cases where people may need to use two providers.

As long as some devs can still download the Nuxt Image module and add the custom config options they need (even if sanity is one of them) I dont see an issue. Personally, even if Nuxt Image is a part of this module package, I'll still probably download Nuxt Image and add it to my nuxt config modules with specific options.

@isakgustavsen
Copy link
Collaborator Author

This should probably only be for the images added inline in the editor so you can use the component as normal

@isakgustavsen
Copy link
Collaborator Author

It may be worth it to just add Nuxt image as a dependency and make this the default behavior. Are there any major downsides to this @danielroe ?

@danielroe
Copy link
Collaborator

I'd prefer not to add it as a dependency.

configuring sanity options for Nuxt Image won't make it the default provider, so it should be safe for the end user, and if we can add an automatic component for images (assuming the user hasn't provided one) that would be amazing.

@isakgustavsen
Copy link
Collaborator Author

Content component now renders images automatically. Im not sure if we want this to only be if Nuxt Image is installed, but there is a flag for it created at build time so it's easy to do so

@isakgustavsen
Copy link
Collaborator Author

Im unsure if the right approach is to use crop and hotspot if it's passed, so to avoid confusion this strips it of those two. All other props defined in the image block will be passed on

@isakgustavsen isakgustavsen changed the title Feat: Implement nuxt image feat: Implement nuxt image Aug 1, 2025
@isakgustavsen isakgustavsen requested a review from danielroe August 4, 2025 14:17
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 10, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@nuxtjs/sanity@1255

commit: 0f7835a

Comment on lines +211 to +218
if (typeof ImageComponent !== 'string') {
return h(ImageComponent, {
provider: 'sanity',
...attrs,
...nuxtImgOptions.value,
src: props.assetId,
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this operates at runtime, meaning (I think) we aren't able to tree-shake this out.

instead, I think we can split this into two separate files, and pick which one to add within the module

},
async setup(options, nuxt) {
if (hasNuxtModule('@nuxt/image', nuxt)) {
(options as SanityModuleOptions).isNuxtImageEnabled = true
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's not set this on the options, but just use a let isNuxtImageEnabled = false

we also shouldn't detect this based on the presence of the module, but instead by doing something like this, as we do below in L372+:

https://github.com/nuxt-content/mdc/blob/d426839cfac357eea974d468d2aad0e51fdea9a2/src/module.ts#L181-L189

@isakgustavsen isakgustavsen marked this pull request as draft November 27, 2025 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add serializer to block to handle Images from sanity feat: implement image handling with @nuxt/image

3 participants