next/image不从外部URL加载图像

0yg35tkg  于 2023-06-22  发布在  其他
关注(0)|答案(2)|浏览(146)

我想从cdn获取镜像,但next报告错误主机名****没有在你的next.config.js镜像下配置。根据next官网,我在next.config.js中添加了以下配置,但不起作用。

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

下一个版本是12.3.1。我应该怎么做才能正确地获得图像?

sdnqo3pr

sdnqo3pr1#

如果你只打算使用域的每一条路径,你不需要remotePatterns
相反,您可以使用domains

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

此外,事实上,你的代码片段是不工作的我似乎是因为你修改了你的next.config.js文件,但还没有重新启动服务器,如果不是,在每次更改配置文件后,你必须停止服务器,并重新启动它与npm run dev查看更改

xoefb8l8

xoefb8l82#

这个问题已经解决了,因为我在后面的代码中将图像的域设置为[]

相关问题