Next JS and Cloudinary, images not loading

coo*_*ita 2 javascript cloudinary next.js

我正在学习 Next JS,并且正在尝试将我的应用程序导出为静态站点。结果,我使用 Cloudinary 来处理图像,问题是它们没有显示。接下来的JS似乎在URL中添加了一些参数,这破坏了图像,下面是一个例子:

https://res.cloudinary.com/dnliygel/image/upload/v1624031405/next-tutorial/f_auto,c_limit,w_384,q_auto/images/profile_ileph2.jpg(不起作用)

https://res.cloudinary.com/dnliygel/image/upload/v1624031405/next-tutorial//images/profile_ileph2.jpg(有效)

似乎路径中额外的f_auto,c_limit,w_384,q_auto打破了它。

这是因为我有一个免费的 Cloudinary 帐户吗?我该如何解决?

作为参考,这是我的next.config.js

module.exports = {
  basePath: '/out',
  assetPrefix: '/out',
  images: {
    loader: 'cloudinary',
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
    path: 'https://res.cloudinary.com/dnliyglel/image/upload/v1624031405/next-tutorial/',
  },
    exportPathMap: async function() {
    const paths = {
      '/': { page: '/' },
      '/posts/first-post': { page: '/posts/first-post'}
      };
      return paths; 
    }
  };
Run Code Online (Sandbox Code Playgroud)

以下是如何将其添加到组件的示例:

<Image
  priority
  src="/images/profile_ileph2.jpg"
  className={utilStyles.borderCircle}
  height={144}
  width={144}
  alt={name}
/>
Run Code Online (Sandbox Code Playgroud)

小智 7

包含转换时资产的 URL 不正确。

https://res.cloudinary.com/dnliyglel/image/upload/v1624031405/next-tutorial/f_auto,c_limit,w_384,q_auto/images/profile_ileph2.jpg

v1624031405public_id () 的版本号 ( ) 和文件夹部分next-tutorial在转换之前,而 public_id ( images/profile_ileph2) 的其余部分在转换之后。

将它们移动到正确的位置意味着图像按预期加载: https://res.cloudinary.com/dnliygel/image/upload/f_auto,c_limit,w_384,q_auto/v1624031405/next-tutorial/images/profile_ileph2.jpg

您可以尝试更改以下path内容:

https://res.cloudinary.com/dnliyglel/image/upload/v1624031405/next-tutorial
Run Code Online (Sandbox Code Playgroud)

到:

https://res.cloudinary.com/dnliyglel/image/upload/
Run Code Online (Sandbox Code Playgroud)

然后将完整的 public_id (包括next-tutorial文件夹)包含在<Image/>组件的src. 例如

src="/next-tutorial/images/profile_ileph2.jpg"
Run Code Online (Sandbox Code Playgroud)

有关 Cloudinary URL 结构的更多详细信息,请参阅文档的以下部分: https: //cloudinary.com/documentation/image_transformations#transformation_url_syntax