0

I built a Chrome extension that is fetching some JSON file from my website

function updateTestReport()
{
    fetch(http://somewebsite.com/file.json)
    .then(response => response.json())
    .then(data => {   

       //logic
    }); 
}

And it works fine, however, lets say I updated my website (including file.json) but now still "old" version is being fetched, unless I refresh my website using CTRL + F5, is there way to fetch LATEST version of the file?

Matthewek
  • 1,519
  • 2
  • 22
  • 42

1 Answers1

1

You can prevent caching of the json file by adding a query string

function updateTestReport()
  {
    fetch(http://somewebsite.com/file.json?vr=1.0)
    .then(response => response.json())
    .then(data => {   

       //logic
    }); 
}
brk
  • 48,835
  • 10
  • 56
  • 78
  • So in this case adding ?vr=1.0 indicates latest version? Thank you for answer – Matthewek Mar 04 '19 at 14:45
  • @Matim — No. Changing the URL means the browser won't have a cached copy of the data the URL points to. There is nothing special about `?vr=1.0`, and it will cause the browser to cache the file against that new URL. You need to change the URL every time you want to load new data. Adding a static query string, as this answer proposes, **won't work**. – Quentin Mar 04 '19 at 14:47
  • @Matim just want to add on top of Quentin answer. You can bump up the number when you are joining any major change – brk Mar 04 '19 at 14:49
  • @brk — Publishing a new version of the Chrome extension and expecting people to update to it every time the data changes on the website seems unreasonable. – Quentin Mar 04 '19 at 14:50
  • So If I understand correctly, I could create a global variable, and everytime I fetch, I would do myVar++ and in the link I could use: 'http://somewebsite.com/file.json?vr=' + myVar; ? Thank you – Matthewek Mar 04 '19 at 14:51
  • I believe I found correct (in my exact usage case) to do it, fetching with 'no cache' as answered here: https://stackoverflow.com/questions/29246444/fetch-how-do-you-make-a-non-cached-request – Matthewek Mar 04 '19 at 14:57