Skip to content

Instantly share code, notes, and snippets.

@lonekorean
Created March 22, 2015 03:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonekorean/46ebad9a941585cf32d4 to your computer and use it in GitHub Desktop.
Save lonekorean/46ebad9a941585cf32d4 to your computer and use it in GitHub Desktop.
Auto cache-buster
window.addEventListener('keydown', function(e) {
if (e.keyCode === 116 || (e.ctrlKey && e.keyCode === 82)) { // f5 or ctrl + r
// parse URL
var split = window.location.href.split('#');
var url = split[0];
var hash = split[1];
// set cache-busting query string param
var pair = 'cachebuster=' + Date.now();
if (url.indexOf('cachebuster=') > -1) {
url = url.replace(/cachebuster=\d+/, pair);
} else {
url += (url.indexOf('?') > -1 ? '&' : '?') + pair;
}
// reassemble
var final = url + (hash ? '#' + hash : '');
window.location.href = final;
e.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment