Last active
May 19, 2018 04:28
-
-
Save rudyhuynh/5ba68a5b657e4a42176e3c3d2f9ee729 to your computer and use it in GitHub Desktop.
A higher-order fetch that bypass cache
This file contains 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
// 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