Skip to content

Instantly share code, notes, and snippets.

@cornedor
Last active December 29, 2015 15:19
Show Gist options
  • Save cornedor/7689606 to your computer and use it in GitHub Desktop.
Save cornedor/7689606 to your computer and use it in GitHub Desktop.
Gets Last-Modified header from server and if changed reloads the style sheet.
(function(timeout, urls) {
var times = {};
setInterval(function() {
for(var i = 0; i < urls.length; i++) {
var x = new XMLHttpRequest(),
url = urls[i];
x.url = url;
x.open('HEAD', url + (url.indexOf("?") === -1 ? "?v=" : "&v=") + Date.now(), true);
x.onreadystatechange = function(e) {
var x = e.target;
if(x.readyState === 4 && x.status === 200) {
var time = new Date(x.getResponseHeader('Last-Modified'));
if(time.toString() !== 'Invalid Date') {
if(times[e.target.url] === undefined || times[e.target.url].toString() !== time.toString()) {
document.querySelector("link[href^=\"" + e.target.url + "\"]").setAttribute("href", e.target.url);
times[e.target.url] = time;
}
}
}
}
x.send();
}
}, timeout);
}(2000, ["asset/css/edetail.css", "asset/css/app.css"]));
!function(e,t){var a={};setInterval(function(){for(var e=0;e<t.length;e++){var r=new XMLHttpRequest,s=t[e];r.url=s,r.open("HEAD",s+(-1===s.indexOf("?")?"?v=":"&v=")+Date.now(),!0),r.onreadystatechange=function(e){var t=e.target;if(4===t.readyState&&200===t.status){var r=new Date(t.getResponseHeader("Last-Modified"));"Invalid Date"!=""+r&&(void 0===a[e.target.url]||""+a[e.target.url]!=""+r)&&(document.querySelector('link[href^="'+e.target.url+'"]').setAttribute("href",e.target.url),a[e.target.url]=r)}},r.send()}},e)}(2e3,["asset/css/edetail.css","asset/css/app.css"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment