Bust Cache Front-end Development

Busting caching while working on CSS file, that JS changes the CSS path with current date at the end when called.

function refreshCSS() {
	var sheets = [].slice.call(document.getElementsByTagName("link"));
	var head = document.getElementsByTagName("head")[0];
	for (var i = 0; i < sheets.length; ++i) {
		var elem = sheets[i];
		head.removeChild(elem);
		var rel = elem.rel;
		if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
			var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
			elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
		}
		head.appendChild(elem);
	}
}	

Start the Discussion!Leave a Reply