【问题标题】:How can I configure an external image loader in next.js properly?如何在 next.js 中正确配置外部图像加载器?
【发布时间】:2021-10-04 00:17:33
【问题描述】:

我正在使用 next.js 导出来创建一个托管在谷歌云存储上的静态 html 导出。

我在 next.js 配置中使用此解决方法。

images: {
 loader: 'imgix',
 path: 'https://noop/',
},

我需要如何配置外部加载程序,例如imgix 可以正常工作吗?

【问题讨论】:

    标签: next.js imgix


    【解决方案1】:

    相对路径

    images: {
        deviceSizes: [320, 420, 768, 1024, 1200],
        loader: "imgix",
        path: "https://<account>.imgix.net/",
    },
    

    然后在你的组件中,

    const imgSrc = "random.png";
    
    <Image
         src={imgSrc} // <= https://<account>.imgix.net/random.png
         width={300}
         height={300}
         alt={alt}
       />
    

    绝对路径

    images: {
        deviceSizes: [320, 420, 768, 1024, 1200],
        loader: "imgix",
        path: "",
    },
    

    然后在你的组件中,

    const imgSrc = "https://<imagesource-domain>/random.png";
    
    <Image
         src={imgSrc} // <= https://<imagesource-domain>/random.png
         width={300}
         height={300}
         alt={alt}
       />
    

    【讨论】:

    • 好的,但这意味着 random.png 必须单独上传到 imgix,对吗?我一直以为nextjs会在编译时优化图片。
    • 是和不是。如果你想使用云提供商来优化图像而不是使用 Next.js 的内置图像优化,你可以配置加载器和路径前缀。即imgix
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多