Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs: Unable to load images from static folder

Tags:

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.

like image 780
Vaggelis Stefanakis Avatar asked Mar 22 '18 17:03

Vaggelis Stefanakis


People also ask

How do I serve a static file in NextJS?

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 ( / ).

How access public folder in next JS?

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.


1 Answers

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.

like image 110
Tomasz Mularczyk Avatar answered Oct 26 '22 23:10

Tomasz Mularczyk