Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get "_next/image?url=example.com/assets/photo.png" when I try to load external image from another domain?

I’d like to load pictures from other domain that is "api.domain.com".

I have configured next.config.js like this in order to avoid external URL error.

const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: "http",
        hostname: "api.example.app",
        port: ""
      }
    ]
  }
}

But when I inspect image what I see is that it’s setting srcset="/_next/image?url=http://example.com/assets/photo.png"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I bypass adding _next/image as prefix?

P.s please do not recommend me using img tag.

>Solution :

Next.js automatically optimize images when you use <Image /> from next/image.

Let’s say you have a .png image with 1MB size, and its source is http://example.com/assets/photo.png. Next.js will generate some variants of the image with more modern formats e.g. .webp in different sizes, 0.1 MB, 0.5 MB, and 1 MB. it will collect any src you passed to <Image /> (and exists in the remotePatterns) to change the src to the Next.js built-in optimization loader /_next/image?url=${src}. This way it will respond with the suitable image variant for the device.

Reed more:
https://nextjs.org/docs/app/building-your-application/optimizing/images

You can make your custom loader:
https://nextjs.org/docs/app/api-reference/components/image#loader

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading