Next.js Image组件src属性无效报错:已配置域名仍提示未配置
解决Next.js Image组件提示hostname未配置的问题
重启开发服务器
修改next.config.js后必须重启服务,否则新的图片域名配置不会生效。停止当前运行的开发服务,重新执行npm run dev或yarn dev。修正remotePatterns的pathname格式
你的配置里pathname缺少开头的/,Next.js要求remotePatterns的pathname必须以/开头才能正确匹配所有子路径。修改后的配置如下:const nextConfig = { reactStrictMode: true, swcMinify: true, optimizeFonts: true, images: { remotePatterns: [ { protocol: 'https', hostname: 'img-b.udemycdn.com', pathname: '/**', // 添加开头的斜杠 }, ], }, }; module.exports = nextConfig;适配旧版Next.js(版本<12.3.0)
如果你的Next.js版本低于12.3.0,remotePatterns特性还未推出,需要改用domains配置:const nextConfig = { reactStrictMode: true, swcMinify: true, optimizeFonts: true, images: { domains: ['img-b.udemycdn.com'], }, }; module.exports = nextConfig;
内容的提问来源于stack exchange,提问作者Mohammad Abbass
相关产品推荐
相关产品推荐

