Created
August 31, 2019 16:46
-
-
Save biswajitpaul01/a1fafce1523fd28f3fa703e915eec4d1 to your computer and use it in GitHub Desktop.
Browser Cache API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://blog.logrocket.com/beyond-cookies-todays-options-for-client-side-data-storage/ | |
const apiRequest = new Request('https://www.example.com/items'); | |
caches.open('exampleCache') // opens the cache | |
.then(cache => { | |
cache.match(apiRequest) // checks if the request is cached | |
.then(cachedResponse => | |
cachedResponse || // return cachedReponse if available | |
fetch(apiRequest) // otherwise, make new request | |
.then(response => { | |
cache.put(apiRequest, response); // cache the response | |
return response; | |
}) | |
}) | |
.then(res => console.log(res)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment