如何在使用插件时为“next/image”添加域到 next.config.js

Posted

技术标签:

【中文标题】如何在使用插件时为“next/image”添加域到 next.config.js【英文标题】:how to add domains to next.config.js for "next/image" while using a plugin 【发布时间】:2021-06-27 23:52:35 【问题描述】:

这是我目前的设置。

// next.config.js
const withImages = require("next-images");

module.exports = withImages(
  webpack(config, options) 
    return config;
  ,
);

我想添加此代码以允许来自域 localhost:3001 的图像。

images: 
  domains: ['localhost:3001'],
,

【问题讨论】:

【参考方案1】:

我认为你应该设置assetPrefix,阅读docs中的选项

// next.config.js
const withImages = require('next-images')
module.exports = withImages(
  assetPrefix: 'https://example.com',
  dynamicAssetPrefix: true,
  webpack(config, options) 
    return config
  

【讨论】:

【参考方案2】:

您只需将images 对象添加到传递给withImages 的配置对象中。

// next.config.js

const withImages = require("next-images");

module.exports = withImages(
    images: 
        domains: ['localhost:3001']
    ,
    webpack(config, options) 
        return config;
    
);

【讨论】:

以上是关于如何在使用插件时为“next/image”添加域到 next.config.js的主要内容,如果未能解决你的问题,请参考以下文章