Skip to content

Instantly share code, notes, and snippets.

@biswajitpaul01
Created August 31, 2019 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biswajitpaul01/a1fafce1523fd28f3fa703e915eec4d1 to your computer and use it in GitHub Desktop.
Save biswajitpaul01/a1fafce1523fd28f3fa703e915eec4d1 to your computer and use it in GitHub Desktop.
Browser Cache API
// 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