-
Notifications
You must be signed in to change notification settings - Fork 36
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
How to integrate with @sanity/image-url? #19
Comments
Try this: // Nuxt plugin
import imageUrlBuilder from '@sanity/image-url'
const configSanity = {
projectId: 'tufjlt9c',
dataset: 'production',
}
export default ({ $sanity }, inject) => {
const builder = imageUrlBuilder($sanity.client)
function urlFor(source) {
return builder.image(source)
}
inject('urlFor', urlFor)
} There is also |
thanks for the quick response 😃 (I'd like to use the crop & hotspot feature of the Studio, which I assume SanityImage can't render) |
@mornir That's a bug - give me a sec. |
@mornir Try with |
Nice! The warning is gone, but now I get this error: ERROR /
server.js:612
useOfficialClient = !!(/*require.resolve*/)(33);
^
SyntaxError: Unexpected token ')'
at new Script (vm.js:88:7)
at C:\projects\movies-web\node_modules\vue-server-renderer\build.prod.js:1:77446
at o (C:\projects\movies-web\node_modules\vue-server-renderer\build.prod.js:1:77509)
at C:\projects\movies-web\node_modules\vue-server-renderer\build.prod.js:1:78172
at new Promise (<anonymous>)
at C:\projects\movies-web\node_modules\vue-server-renderer\build.prod.js:1:78080
at Object.renderToString (C:\projects\movies-web\node_modules\vue-server-renderer\build.prod.js:1:81615)
at SSRRenderer.render (C:\projects\movies-web\node_modules\@nuxt\vue-renderer\dist\vue-renderer.js:3822:38) |
The error occurs when setting the client to minimal. |
Apologies for that. Let's try |
@danielroe Thanks for the quick fix! Back to my original question: import sanity from 'picosanity'
import imageUrlBuilder from '@sanity/image-url'
const client = sanity({
projectId: 'tufjlt9c',
dataset: 'production',
})
const builder = imageUrlBuilder(client)
const urlFor = (source) => builder.image(source).auto('format')
export default (context, inject) => {
inject('urlFor', urlFor)
} |
@mornir I'll need to look into the image URL builder. I think I recall it is only requesting the client to get the configuration, which it's assuming is available - as it doesn't need to perform any fetches. |
Answer is don't pass in the client, just the config, which image builder supports as well. Or we can modify the minimal client to return its config via the See https://www.github.com/sanity-io/image-url/tree/master/src%2Fbuilder.ts |
Oh I see 😯 import imageUrlBuilder from '@sanity/image-url'
export default ({ $sanity }, inject) => {
const builder = imageUrlBuilder($sanity) // image-url will automatically look for the clientConfig key, right?
function urlFor(source) {
return builder.image(source)
}
inject('urlFor', urlFor)
} Now I'm importing the Sanity config from the Nuxt config: import imageUrlBuilder from '@sanity/image-url'
import config from '../nuxt.config.js'
export default (context, inject) => {
const builder = imageUrlBuilder(config.sanity)
function urlFor(source) {
return builder.image(source)
}
inject('urlFor', urlFor)
} I had to explicitly set Now that I think about it, the best would be that the |
I propose exposing the config like so: import imageUrlBuilder from '@sanity/image-url'
export default ({ $sanity }, inject) => {
const builder = imageUrlBuilder($sanity.config)
function urlFor(source) {
return builder.image(source)
}
inject('urlFor', urlFor)
} I'm reluctant to embed |
Good for me 👍 |
... now usable in |
Thanks! |
Hey!
I was hoping I could use the
createClient
to configure the client for the @sanity/image-url package, but it doesn't work 🤔What the best/cleanest of using
@sanity/image-url
with the Nuxt sanity-module?The text was updated successfully, but these errors were encountered: