Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to cache images in react, what should I use for storage

I would like to cache image in react.

<img src='www.myimage/dog.png' />

Every time I load page, dog.png is downloaded from page. I would like to store it in browser with some ttl.

Should I use localStorage, sessionStorage, indexedDb or what is the best place to store and why?

Thank you!

like image 865
masiyik879 Avatar asked Sep 05 '25 02:09

masiyik879


1 Answers

There's no need to cache your images yourself. Unless you're doing some specialised lazy-loading, the browser will automatically cache it for you.

They will still "load", but from cache. And they may be at the browser's discretion.

To confirm, open dev tools > network > Img and see how the images are loaded from cache, instead of an endpoint.

enter image description here

like image 62
Obed Parlapiano Avatar answered Sep 08 '25 01:09

Obed Parlapiano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!