Last active
May 19, 2018 14:21
-
-
Save rudyhuynh/2ebf4786013dbb177bc23d1303c7d1d8 to your computer and use it in GitHub Desktop.
First abroach
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
// 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