How do I cache data that the page has already requested?

requirements: after the data is successfully loaded through ajax, what if the user refreshes the page and caches the requested data? Instead of requesting back-end data again. Compatible with IE8 and above


to be compatible with ie8, put it in cookie .
localstorage do not know whether it is compatible with ie8 or not, check it out for yourself.
IE9 and above are compatible.


you can store the data in localStorage or sessionStorage , and then judge that if there is data in the cache, you can directly fetch the cached data, otherwise request the data

.
if(localStorage.cacheData){
    this.data = localStorage.cacheData
}else {
    fetchData().then(res => {
        ...
    })
}

simply use cookie, 's current most compatible mode, but with a little less capacity
it's better to be in localStorage or sessionStorage if possible
if you develop app, you can write your own local files, databases, and so on.

Menu