zlr
zlr

Reputation: 1

next/image does not load images from external URL

I want to get images from cdn, but next reports error hostname **** is not configured under images in your next.config.js.According to the next official website, I added the following configuration in next.config.js, but it doesn’t work.

// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'assets.example.com',
        port: '',
        pathname: '/**',
      },
    ],
  },
}

Next version is 12.3.1. What should I do to get the image correctly?

Upvotes: 0

Views: 989

Answers (2)

zlr
zlr

Reputation: 1

It has been solved, because I set the domains of images to [] in the code behind

Upvotes: 0

eroironico
eroironico

Reputation: 1372

You don't need remotePatterns if you just plan to use every path of a domain.

Instead, you can use domains:

// next.config.js
module.exports = {
    images: {
        domains: ["assets.example.com"]
    }
}

Also, the fact that your snippet is not working seems to me as you had modified your next.config.js file but haven't restarted the server, if not, after every change to the config file you must stop the server and restart it with npm run dev to see the changes

Upvotes: 0

Related Questions