Skip to content

Instantly share code, notes, and snippets.

@rudyhuynh
Last active May 19, 2018 14:21
Show Gist options
  • Save rudyhuynh/2ebf4786013dbb177bc23d1303c7d1d8 to your computer and use it in GitHub Desktop.
Save rudyhuynh/2ebf4786013dbb177bc23d1303c7d1d8 to your computer and use it in GitHub Desktop.
First abroach
// this function accepts the same input and returns the same output as window.fetch(),
// plus, it adds a ramdom param into the URL to bypass cache.
const bypassCacheFetch = (input, init) => {
let bypassCacheUrl
if (input.includes('?') {
bypassCacheUrl = input + '&_v=' + Math.random()
} else {
bypassCacheUrl = input + '?_v=' + Math.random()
}
return fetch(bypassCacheUrl, init)
}
// usage:
async getData(){
const response = await bypassCacheFetch('http://my.api.com/data.json')
const json = await response.json()
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment