Nextjs Image an issue with "loader" property that does not implement width
Asked Answered
C

3

7

This is my code for the Nextjs Image component:

...  Cell: ({ value }: CellType) => (
  <Image
    priority
    src={value}
    loader={() => value}
    className={''}
    height={100}
    width={100}
    alt={'profile_picture'}
  />
),

enter image description here

Does it mean I need a custom loader function to get rid off the warning? Thanks!

Clairvoyant answered 20/2, 2022 at 10:4 Comment(2)
If you're not applying any optimisations through a 3rd party cloud provider (your loader isn't doing anything), you might as well just use unoptimized={true} on the Image.Irrespirable
@Irrespirable that actually makes sense. Thank you! I'll update my Q!Clairvoyant
L
10

I had a same issue like yours and solved by adding unoptimized={true} prop into the <Image> tag. If you don't apply optimizations, you need to announce that you don't optimize.
Something like below:

<Image
    priority
    src={value}
    loader={() => value}
    unoptimized={true}  // <=== insert this prop
    className={''}
    height={100}
    width={100}
    alt={'profile_picture'}
  />

Please let me know if it work.
Thank you.

Luellaluelle answered 11/6, 2022 at 13:48 Comment(0)
R
3

I had the same error but what I have done to fix it is to incorporate in the next.config.js file the URL of my media files, in my case it is cloudinary, the file should look like this:


/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,

  images: {
    domains: [
      'res.cloudinary.com'
    ],
  },
}

module.exports = nextConfig

And when I use the image component (next/image) I remove the loader and just put the URL like this:

<Image
  key={element.id}
  src={element.url}
  width={500}
  height={500}
  alt="some description"
/>

I don't use unoptimized={true} because the point is that the component changes the quality of the image according to the client's device.

good look :)

Ramonramona answered 5/7, 2022 at 17:47 Comment(0)
W
3

I fixed it like this:

Added this definition to loader attribute-:

loader={({ src, width }) => { return src + "?w=" + width }}

<Image
    priority
    src={value}
    loader={({ src, width }) => { return src + "?w=" + width }}
    height={100}
    width={100}
    alt={'profile_picture'}
/>
Winn answered 27/6, 2023 at 4:26 Comment(1)
Do note that some external URLs might return an error if you add ?w= to the query string when they're very strict. For example, an URL that has the image specified in the query string itself, e.g. foo.com/image?image_hash=asdfasdfCruz

© 2022 - 2024 — McMap. All rights reserved.