How can I load images in a component in Next.js? Do I have to build the project first? If yes, is there a way to load the images without building first? I cannot get this to work, no matter what I try.
Next. js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced by your code starting from the base URL ( / ).
NextJS public folder static files the files in the public folder are accessible with the base url. For example, if you placed the robots. txt file in the public folder, It is accessible with the localhost:3000/robots. txt file.
from the docs:
Next.js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced by your code starting from the base URL (/).
So, first add an image to public/my-image.png
and then you can reference it:
<img src="/my-image.png" />
I think next.js will have a watch on this directory so you won't need to restart
your server every time you put something in there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With