Nextjs,公共文件夹中的图像在部署时未找到,但在本地找到 [英] Nextjs, Images in public folder not found on deploy, but are found locally

查看:26
本文介绍了Nextjs,公共文件夹中的图像在部署时未找到,但在本地找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地开发时,将 /public 文件夹放在 /src/ 中时会找到图像:

When I'm developing locally the images are found when I place the /public folder in /src/:

# locally
./src/public/images/

然后,当我进行部署时,找不到图像.但是当我将公用文件夹放在 src 之外时,会发现图像:

Then when I do a deploy the images are not found. But when I place the public folder outside of src the images are found:

# deploy
./public/images/
./src/

我将图像用作:

<img src="/images/my-image.jpg" alt="" />

有我必须使用的配置设置吗?

Is there a configuration setting I have to use?

完整结构:

|- .now/
|  |-  project.json
|  └── README.txt  
|- next.config.js
|- now.json
|- src/
|  |- .next/
|  |  |-  build-manifest.json
|  |  |-  react-loadable-manifest.json
|  |  |-  cache/
|  |  |-  server/
|  |  └── static/
|  |-  pages/
|  └── next-env.d.ts

推荐答案

公用文件夹必须在根目录下.否则无法配置.

The public folder has to be in the root. There’s no way to configure otherwise.

然后,您的代码可以从基本 URL (/) 开始引用公共内部的文件./public/path/image.jpg 用作 /path/image.jpg

Files inside public can then be referenced by your code starting from the base URL (/). /public/path/image.jpg is served as /path/image.jpg

https://nextjs.org/docs/basic-features/static-文件服务

这篇关于Nextjs,公共文件夹中的图像在部署时未找到,但在本地找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆