Search code examples
google-chromehttpbrowserfetchbrowser-cache

Disable cache for certain resource programmatically


I have this setting in Chrome devtools:

enter image description here

this setting works for me. However, I also want to disable the cache for certain resources. My question is - is there a way to disable the cache for a resource when you use fetch?

fetch(url).then(v => {});

is there some option or header that we can use to prevent the browser from using the cache to retrieve the resource?


Solution

  • This has the answers I was looking for:

    https://hacks.mozilla.org/2016/03/referrer-and-cache-control-apis-for-fetch/

    first make sure caching is disabled on the server, if necessary.

    Then in the browser, we can use one of these:

    fetch(url, {cache: 'no-store'})
    

    or

    fetch(url, {cache: 'no-cache'})
    

    and get the result we want to achieve.