admin管理员组文章数量:1406149
I have a simple image in public folder:
public\large_logo.png
I keep trying to display it but it does not show up:
<Image
src="/large_logo.png"
alt="Logo"
width={40}
height={40}
className={cn(
"aspect-square h-fit flex-none rounded-full bg-secondary object-cover sm:ms-auto",
)}
/>
I tried to set a link to an external image in the src and it works.
So I'm guessing the problem is with the image placement.
Anyone has any idea what's going on?
I have a simple image in public folder:
public\large_logo.png
I keep trying to display it but it does not show up:
<Image
src="/large_logo.png"
alt="Logo"
width={40}
height={40}
className={cn(
"aspect-square h-fit flex-none rounded-full bg-secondary object-cover sm:ms-auto",
)}
/>
I tried to set a link to an external image in the src and it works.
So I'm guessing the problem is with the image placement.
Anyone has any idea what's going on?
- Can you show your project folder structure? – Olaf Commented Mar 6 at 11:59
2 Answers
Reset to default 2Possible solution:
The most common reasons for this issue are:
Filename and Path Mismatch:
- Double-check the filename and path. Ensure the filename
large_logo.png
matches exactly (case-sensitive) the file in yourpublic
folder. - The path
/large_logo.png
is correct for accessing files directly within thepublic
directory.
- Double-check the filename and path. Ensure the filename
Image
Component Dimension Requirements:- The Next.js
Image
component requireswidth
andheight
attributes. Ensure these are accurately set.
- The Next.js
Image File Issues:
- Test with a different, simpler image (e.g., a basic JPEG or PNG). This will isolate whether the issue is with the specific image file.
Development Server Restart:
- If you added the image to the
public
folder after your development server started, restart the server (npm run dev
oryarn dev
).
- If you added the image to the
Browser Developer Tools (Network Tab):
- Open your browser's developer tools (F12) and go to the "Network" tab.
- Refresh the page and look for
large_logo.png
. - A 404 status code indicates the file isn't found.
Test with a Standard
<img>
Tag:- Replace the
Image
component with a standard<img>
tag:
<img src="/large_logo.png" alt="Logo" width="40" height="40" />
- If the
<img>
tag works, the issue lies specifically with yourImage
component usage.
- Replace the
Maybe this one will work:
import Image from 'next/image';
import cn from 'classnames';
export default function MyComponent() {
return (
<Image
src="/large_logo.png"
alt="Logo"
width={40}
height={40}
className={cn(
"aspect-square h-fit flex-none rounded-full bg-secondary object-cover sm:ms-auto",
)}
/>
);
}
Do you use anything like Chakra UI
or something like that?
Images under /public can be shown directly from URL. Try to open it and see if is visible. If so, may is do to some CSS classes or something about Image tag. Did you tryied to use the tag (the classic tag and not the component) to see if it work?
本文标签: reactjsNextJs Displaying an image from public folder does not workStack Overflow
版权声明:本文标题:reactjs - NextJs Displaying an image from public folder does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744977901a2635632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论