Next.js Discord

Discord Forum

Dynamic Images

Answered
Erenay posted this in #help-forum
Open in Discord
Avatar
The images in the new public folder won't load until I build the site the next time. How can I use these images dynamically?
Answered by joulev
the public folder is read-only, you cannot save images there. you must save images elsewhere – for example, in a s3 bucket
View full answer

13 Replies

Avatar
@Erenay The images in the new public folder won't load until I build the site the next time. How can I use these images dynamically?
Avatar
you can load images dynamically by just referencing to it:
<img src='/your/file.png' />

Like that the image will be automatically loaded and you can dynamically change the source if needed
Avatar
@Erenay So I will use `<img>` instead of `<Image>`. Do I understand correctly?
Avatar
this was just to clarify, what's important. It could also look like this:
<Image src='/your/file.png' />
Avatar
@B33fb0n3 this was just to clarify, what's important. It could also look like this: tsx <Image src='/your/file.png' />
Avatar
It's like this now:

                <Image
                  alt={productData.name}
                  width={192}
                  height={192}
                  className="w-full h-full object-cover"
                  src={productData.banner}
                  priority
                  unoptimized
                />


Next.js version: 14.2.22
After I build and run the site, I put the file correctly in the public folder and the image does not appear until the next build. But it appears when I rebuild the site.
Avatar
@Erenay The images in the new public folder won't load until I build the site the next time. How can I use these images dynamically?
Avatar
the public folder is read-only, you cannot save images there. you must save images elsewhere – for example, in a s3 bucket
Answer
Avatar
any changes to the public folder are only reflected in the next build
Avatar
thank you very much
Avatar
@joulev the public folder is read-only, you cannot save images there. you must save images elsewhere – for example, in a s3 bucket
Avatar
So, for example, can I store the images in a different folder in my server other than the public directory?
Is it dynamic that way?
Avatar
@Erenay So, for example, can I store the images in a different folder in my server other than the public directory?
Avatar
yea... you can save it anywhere. you can even save it inside public as well if you want – but for them to be accessible, you need to create some sort of route handler using fs to read the file system and return the file as a response
the only restriction is that if you save it in public, it won't be available automatically the way existing files in public are