T
TanStack3y ago
absent-sapphire

How do I write out my cached data locally and update it every 30 days from the server via fetch ...

I need to fetch data from a server, store it locally "by writing out the cache", and update it every 30 days. This needs to be done in JS and React CLIENT side. I don't want to create any servers or anything like that. I don't even know where I'm going to host this app. Basically, I only want to hit the server once every 30 days and use the local json file the other 29 days. Sample Fetch URL : https://pokeapi.co/api/v2/pokemon-species?limit=1
3 Replies
conscious-sapphire
conscious-sapphire3y ago
There are many local storage apis available (localStorage, indexedDB, Cache, FileSystem). But the one that is the most "made for" retaining remote responses locally is the Cache API from a Service Worker. This is specifically made for offline behavior (or similar needs).
absent-sapphire
absent-sapphireOP3y ago
There a good resource for using the Cache API? You can store the data on disk, inside a client side app?
conscious-sapphire
conscious-sapphire3y ago
It's kind of on disk, it's through the browser's APIs so you don't really have filesystem access, but it is persistent. My recommendation would be to search for stuff on MDN, they always have good explanations of these sort of concepts.

Did you find this page helpful?