Next.js 13.0.5中Next/Image已配置外部主机仍报错求助
解决Next.js 13.0.5中Next/Image加载CloudFront图片的配置问题
针对你遇到的已配置remotePatterns但仍报错的情况,按以下步骤排查:
- 重启开发服务器:修改
next.config.js后,必须重启Next.js开发服务才能让新配置生效,这是最容易忽略的点。 - 简化
remotePatterns配置:去掉port字段(HTTPS默认使用443端口,无需显式指定),修改后的配置如下:const nextConfig = { reactStrictMode: true, swcMinify: true, images: { remotePatterns: [ { protocol: "https", hostname: "d2ss8eaptdxs3l.cloudfront.net", pathname: "/**", }, ], }, } module.exports = nextConfig - 尝试使用
domains配置:虽然remotePatterns是12.3.0+推荐的方式,但部分场景下domains配置更稳定,替换成以下配置试试:const nextConfig = { reactStrictMode: true, swcMinify: true, images: { domains: ["d2ss8eaptdxs3l.cloudfront.net"], }, } module.exports = nextConfig - 清除缓存并重启:删除项目根目录下的
.next文件夹,清除缓存后再启动开发服务器,避免旧配置残留。 - 确认配置文件位置:确保
next.config.js在项目根目录(而非src子目录),否则Next.js无法读取到配置。
另外注意到你代码中图片路径是profile_pictures(下划线),但报错信息里的路径是profile-pictures(横杠),虽然pathname: "/**"会匹配所有子路径,但建议保持代码中路径与实际文件路径一致,避免其他潜在问题。
内容的提问来源于stack exchange,提问作者Hajimaru
相关产品推荐
相关产品推荐

