-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-js.min.js
5 lines (5 loc) · 2.36 KB
/
cache-js.min.js
1
2
3
4
5
/*
* © Aymane Taibi 2016
* github/nimda95/cacheres
* mini script used to lad pages faster by storing resource files in HTML5's localStorage, and then loading them later from it.
*/var cacheJs={config:null,loadedCounter:0,resourcesCounter:0,initializeDone:function(){console.info("cacheJs finished loading all files"),cacheJs.config.onFinished()},initialize:function(a){cacheJs.getConfig(a)},getConfig:function(a){var b=localStorage.getItem("cacheJs.localStorageConfig");cacheJs.config=a,null===b?(localStorage.setItem("cacheJs.localStorageConfig",JSON.stringify(cacheJs.config)),cacheJs.checkFiles(cacheJs.config,cacheJs.config)):cacheJs.checkFiles(cacheJs.config,JSON.parse(b))},checkFiles:function(a,b){for(var c in a.files)a.files.hasOwnProperty(c)&&cacheJs.resourcesCounter++;for(var c in a.files)void 0!==a.files[c]?b!=={}&&void 0!==b.files[c]&&null!==localStorage.getItem(c)&&b.files[c].version===a.files[c].version&&"-"!==a.files[c].version?cacheJs.checkIfLoaded():cacheJs.getFile(c,a.files[c]):void 0!==a.files[c]&&null!==localStorage.getItem(c)&&cacheJs.getFile(c,a.files[c]);localStorage.setItem("cacheJs.localStorageConfig",JSON.stringify(a))},injectFile:function(a,b){var c=document.createElement(b.type);c.innerHTML=localStorage.getItem(a);for(var d in b.attributes)void 0!==b.attributes[d]&&c.setAttribute(b.attributes[d].name,b.attributes[d].value);document.head.appendChild(c)},checkIfLoaded:function(){++cacheJs.loadedCounter===cacheJs.resourcesCounter&&cacheJs.injectAllFiles()},getFile:function(a,b){var c=new XMLHttpRequest;c.onreadystatechange=function(){if(4===c.readyState)if(-1!==[304,302,200,201,202].indexOf(c.status))localStorage.setItem(a,"/*"+a+"*/\n"+c.responseText),cacheJs.checkIfLoaded();else if(0===c.status){var d=document.createElement(b.type);switch(b.type){case"style":d.setAttribute("href",b.uri),d.setAttribute("rel","stylesheet");break;case"script":d.setAttribute("src",b.uri);break;default:console.warn("Unknown resource type : "+b.type)}d.onload=function(){cacheJs.checkIfLoaded()};for(var e in b.attributes)void 0!==b.attributes[e]&&d.setAttribute(b.attributes[e].name,b.attributes[e].value);document.head.appendChild(d)}else console.warn("Unable to load Resource : "+a+"(response code "+c.status+")",b.uri)},c.open("GET",b.uri,!0),c.send()},injectAllFiles:function(){for(var a in cacheJs.config.files)cacheJs.injectFile(a,cacheJs.config.files[a]);cacheJs.initializeDone()}};