【问题标题】:How do I use an image from an external domain in Next.js?如何在 Next.js 中使用来自外部域的图像?
【发布时间】:2022-01-31 02:00:44
【问题描述】:

我正在尝试在引用外部域的 Next.js 项目中使用 <Image> 组件。我只是在本地工作。

我被指示将images.domains 添加到我的next.config.js 文件中,但这似乎不起作用。

https://nextjs.org/docs/messages/next-image-unconfigured-host

next.config.js

module.exports = {
  images: {
    domains: ['placehold.it'],
  },
  webpack: (config) => {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    });

    return config;
  },
};

我的头像:

<Image
  width={400}
  height={300}
  src="http://placehold.it/400x300"
/>

我错过了什么吗?

【问题讨论】:

  • 无法重现。您是否尝试过删除 .next 文件夹并重新启动服务器?缓存问题是 Next 经常遇到的麻烦。

标签: next.js nextjs-image


【解决方案1】:

你必须设置一个loader function。

【讨论】:

    【解决方案2】:

    快速查看代码,似乎正确指定了域,但图像源只是域+图像大小。

    存储的图像是否真的有需要包含的文件名? 显然,如果图像仅按大小标记存储,则可能需要缺少文件类型(例如 jpg)。

    如果页面上出现图像占位符,请右键单击以检查元素或单击右键以复制图像地址并将其粘贴到某处以检查整个路径并检查它是否有意义。

    如果使用 Cloudinary 等服务,则需要加载程序。加载器的使用使使用本地图像变得复杂(防止?),但我不知道它会如何影响简单地获取远程图像。

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2022-07-29
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2021-10-02
      • 2011-10-24
      • 2021-09-27
      • 1970-01-01
      相关资源
      最近更新 更多