Skip to content

Instantly share code, notes, and snippets.

@rudyhuynh
Last active May 19, 2018 04:28
Show Gist options
  • Save rudyhuynh/5ba68a5b657e4a42176e3c3d2f9ee729 to your computer and use it in GitHub Desktop.
Save rudyhuynh/5ba68a5b657e4a42176e3c3d2f9ee729 to your computer and use it in GitHub Desktop.
A higher-order fetch that bypass cache
// A higher-order fetch that bypass cache:
export const bypassCacheHOF = (fetch) => (input, init) => {
let bypassCacheUrl
if (input.includes('?') {
bypassCacheUrl = input + '&_v=' + Math.random()
} else {
bypassCacheUrl = input + '?_v=' + Math.random()
}
return fetch(bypassCacheUrl, init)
}
const bypassCacheFetch = bypassCacheHOF(fetch) // <-- pass in the original fetch
// usage:
async getData(){
const response = await bypassCacheFetch('http://my.url')
const json = await response.json()
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment