Skip to content

Commit

Permalink
added es6 library
Browse files Browse the repository at this point in the history
  • Loading branch information
it-ony committed Jul 31, 2017
1 parent f6e8e45 commit deae558
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cssrefresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(function() {
function cssRefresh(link) {
let href = link.href + '?x=' + Math.random();

fetch(href, {
method: 'HEAD'
})
.then(response => {
let lastModified = new Date(response.headers.get('last-modified'));
if (response.ok && (lastModified > link.lastModified || !link.lastModified)) {
link.lastModified = lastModified;
link.element.setAttribute('href', href);
}

setTimeout(() => {
cssRefresh(link);
}, 1000);
})
}

Array.prototype.slice.call(document.querySelectorAll('link'))
.filter(link => link.rel === 'stylesheet' && link.href)
.map(link => {
return {
element: link,
href: link.getAttribute('href').split('?')[0],
lastModified: false
};
})
.forEach(function(link) {
cssRefresh(link);
});
})();

0 comments on commit deae558

Please sign in to comment.