Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent caching of json data #178

Open
systemsplanet-zz opened this issue Aug 29, 2016 · 3 comments
Open

Prevent caching of json data #178

systemsplanet-zz opened this issue Aug 29, 2016 · 3 comments

Comments

@systemsplanet-zz
Copy link

Browsers cache the json returned from a html links

Ideally, Exhibit is using the latest json data.
We can prevent caching by appending a random number:
<link href="senators.json?i=232323.232323" type="application/json" rel="exhibit-data" />

We can generate this HTML dynamically

// create an HTML head link with optional cache buster
function loadExhibitResource(file, opts ){              
  if (opts && opts.cache && opts.cache === 'no')   file += "?i="+(Math.random()*99999999);
  var link  =  document.createElement("link");
  link.href =  file;
  link.type =  'application/json';
  link.rel  =  'exhibit-data';
  document.getElementsByTagName("head")[0].appendChild(link);       
}

loadExhibitResource('senators.json', {cache: no} );
@karger
Copy link
Member

karger commented Aug 29, 2016

This would be great to have but not always---for static exhibits it's going to force lots of wasted fetches. So ideally this would be something you could turn on or off, perhaps by putting a parameter into the link tag e.g. data-ex-cache=false or data-ex-no-cache=true . A pull request would be welcome!

@camilstaps
Copy link

For puristic reasons it would be preferable to use Date.now() instead of a random number:

  • So that you don't stress the OS's entropy source
  • To ensure that the same number does not occur twice

@Bric-e
Copy link

Bric-e commented Jul 4, 2018

Thank you for the trick.
By the way, you need to add quotes to cahe value when calling the method:
loadExhibitResource('senators.json', {cache: 'no'} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants