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
Custom Image Loaders #18606
Comments
Edit: see my next comment instead: #18606 (comment) I also have an idea on how to implement this. Last I checked, non-literal imports in webpack aren't possible so we'd have to figure out a way to get the user's loader into the bundle inside the image component. I think the simplest solution for this would to defer to the bundler and inject via an In the image component, we can require a dummy file that we can resolve to if no custom loader is provided. // /packages/next/client/image-custom-loader.ts
import { ImageLoader } from 'next';
export default null as ImageLoader | null; Then in the image component, we can use the import as if it were either a user loader or just // /packages/next/client/image.tsx
import imageCustomLoader from './image-custom-loader';
// use the loader In the build, we conditionally alias that dummy file in export default async function getBaseWebpackConfig(/* ... */) {
// ...
const resolveConfig = {
const IMAGE_LOADER_ALIAS = /* ... */;
const isCustomLoader = /* ... */;
alias: {
...(isCustomLoader && { [IMAGE_LOADER_ALIAS]: config.images.loader }),
},
};
// ...
} Additionally, we can create a file for each built-in loader and alias to a different destination built-in loader to save a few bytes. |
ref #18450 |
So I've thought about this a bit more and I want to change the solution I'd like. I think y'all should just expose an For example, if I wanted an image component that works for Sanity images, I could create a // SanityImage.js
import Image from 'next/image';
const sanityImageLoader = ({/* ... */}) => {
// ...
};
function SanityImage(props) {
return <Image {...props} imageLoader={sanityImageLoader} />
}
export default SanityImage; |
It looks like the official code is getting developed in #19325. |
This is a #19325 reconfigured to support a loader passed in via a `loader` prop on the Image component, rather than using a config-based approach. The idea is that applications wanting to use a custom loader will create a wrapper element for the image component that incorporates that loader. See a simple example of this pattern in the integration tests. This solution is similar to the one prototyped by @ricokahler in #20213 and described at #18606 (comment) --- Closes #19325 Fixes #18606
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Feature request
The next image component should allow for custom image loaders.
Currently only a few are supported with no way to add a custom one in userland.
Is your feature request related to a problem? Please describe.
In the headless CMS space, there are a good amount of providers that include an image service that transforms images on the fly.
(These were all sponsors of next conf too lol)
Describe the solution you'd like
Edit: I no longer think this is the right solution. See here instead #18606 (comment)
Something like this:Describe alternatives you've considered
I could PR the service I want to be added but I don't think that will scale well. Also adding this feature can make it so y'all can remove the built-in loaders from the image component and reduce bundle size a bit.
The text was updated successfully, but these errors were encountered: