Hi! Currently, I'm working on implementing Imgix img optimization that will work in tandem with Next.js.
Based on the Next.js docs for custom image loaders I created a loader, that passes parameters understandable for the Imgix platform.
export const imageLoader = ({ src, width, quality }) => {
return `${src}&w=${width}&q=${quality || 75}`;
};
It works very well for all images, except for SVG. I found this Prismic blog post Changes to how we handle SVG's, which points out, that SVGs should not have the additional parameters added to them, because they are served without the help of the Imgix platform - fair enough. It also shows how to differentiate between SVGs and other img types. So I changed the code accordingly:
export const imageLoader = ({ src, width, quality }) => {
if (src.includes("images.prismic")) {
return `${src}&w=${width}&q=${quality || 75}`;
}
return src;
};
But because of the lack of those optimization parameters that Next.js expects, I get this warning in the console:
Image with src "https://my-website.cdn.prismic.io/my-website/some_vector_iamge.svg" has a "loader" property that does not implement width. Please implement it or use the "unoptimized" property instead.
Read more: https://nextjs.org/docs/messages/next-image-missing-loader-width
I could add the "unoptimized" property to my Image components, where I use the SVG and it will work. But the thing is, a user in the future could change any image in the Prismic dashboard to the SVG, and it would break in other places.
Do you have an idea how to tackle this problem? I imagine the img service should accept all of those parameters even for SVG and always serve the basic SVG just skipping all params. I think it would be great to check how it's handled by other next.js built-in image loaders.
And if there is no good solution for now, I can create a wrapper component for the Next.js <Image />
, that will check the src
prop and dynamically add the "unoptimized" property based on the domain. That's not ideal since I will duplicate the logic in 2 places, but that's at least some way forward.
I'd love to hear from you, what do you think, and if you already track this issue. Thanks!