Last active
September 20, 2015 10:34
-
-
Save Gerhard-Kanzler/731dd6b86879f0a6865b to your computer and use it in GitHub Desktop.
Css Caching in sessionstorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var css_href_array = []; | |
(function () { | |
"use strict"; | |
// once cached, the css file is stored on the client forever unless | |
// the URL below is changed. Any change will invalidate the cache | |
var styleTags = document.getElementsByClassName('js-style'); | |
function getBrowser() { | |
var myAgent = navigator.userAgent.toLowerCase(); | |
if( myAgent.indexOf('msie') != -1 && parseInt(myAgent.split('msie')[1]) == 8 ){ | |
return 'ie8'; | |
}else{ | |
return 'default'; | |
} | |
} | |
for( var i = 0; i < styleTags.length; i++ ){ | |
if( styleTags[i].getAttribute('data-font') ){ | |
css_href_array.push([styleTags[i].getAttribute('id'), styleTags[i].getAttribute('data-href') + getBrowser() + '.css' ]); | |
}else{ | |
css_href_array.push([styleTags[i].getAttribute('id'), styleTags[i].getAttribute('data-href')]); | |
} | |
} | |
//var css_href = '/static/css/fonts/web-font-' + getBrowser() + '.css'; | |
//css_href_array.push(['font_cache', css_href]); | |
// a simple event handler wrapper | |
function on(el, ev, callback) { | |
if ( el.addEventListener ) { | |
el.addEventListener(ev, callback, false); | |
} else if ( el.attachEvent ) { | |
el.attachEvent("on" + ev, callback); | |
} | |
} | |
// if we have the fonts in sessionStorage or if we've cached them using the native browser cache | |
for( var css in css_href_array ){ | |
if ( (window.sessionStorage && sessionStorage.getItem( css_href_array[css][0] )) || document.cookie.indexOf( css_href_array[css][0] ) > -1 ) { | |
// just use the cached version | |
injectFontsStylesheet(css_href_array[css]); | |
} else { | |
// otherwise, don't block the loading of the page; wait until it's done. | |
on(window, "load", injectFontsStylesheet(css_href_array[css])); | |
} | |
} | |
// quick way to determine whether a css file has been cached locally | |
function fileIsCached(href) { | |
return window.sessionStorage && sessionStorage.getItem(href[0]); | |
} | |
// time to get the actual css file | |
function injectFontsStylesheet( _fileArray ) { | |
// if this is an older browser | |
if ( !window.sessionStorage || !window.XMLHttpRequest ) { | |
var stylesheet = document.createElement('link'); | |
stylesheet.href = _fileArray[1]; | |
stylesheet.rel = 'stylesheet'; | |
stylesheet.type = 'text/css'; | |
document.getElementsByTagName('head')[0].appendChild(stylesheet); | |
// just use the native browser cache | |
// this requires a good expires header on the server | |
document.cookie = _fileArray[0]; | |
// if this isn't an old browser | |
} else { | |
// use the cached version if we already have it | |
//console.log( fileIsCached(_fileArray) ); | |
if ( fileIsCached(_fileArray) ) { | |
injectRawStyle(sessionStorage.getItem(_fileArray[0]), _fileArray[0]); | |
// otherwise, load it with ajax | |
} else { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", _fileArray[1], true); | |
on(xhr, 'load', function () { | |
if ( xhr.readyState === 4 ) { | |
// once we have the content, quickly inject the css rules | |
injectRawStyle(xhr.responseText, _fileArray[0]); | |
// and cache the text content for further use | |
// notice that this overwrites anything that might have already been previously cached | |
sessionStorage.setItem(_fileArray[0], xhr.responseText); | |
//sessionStorage.setItem(_fileArray[0] + '_file', _fileArray[1]); | |
} | |
}); | |
xhr.send(); | |
} | |
} | |
} | |
// this is the simple utitily that injects the cached or loaded css text | |
function injectRawStyle(text, id) { | |
if( document.getElementById(id) ){ | |
document.getElementById(id).innerHTML = text; | |
}else { | |
var style = document.createElement('style'); | |
style.innerHTML = text; | |
document.getElementsByTagName('head')[0].appendChild(style); | |
} | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var css_href_array=[];(function(){"use strict";function t(){var e=navigator.userAgent.toLowerCase();if(e.indexOf("msie")!=-1&&parseInt(e.split("msie")[1])==8){return"ie8"}else{return"default"}}function r(e,t,n){if(e.addEventListener){e.addEventListener(t,n,false)}else if(e.attachEvent){e.attachEvent("on"+t,n)}}function s(e){return window.sessionStorage&&sessionStorage.getItem(e[0])}function o(e){if(!window.sessionStorage||!window.XMLHttpRequest){var t=document.createElement("link");t.href=e[1];t.rel="stylesheet";t.type="text/css";document.getElementsByTagName("head")[0].appendChild(t);document.cookie=e[0]}else{if(s(e)){u(sessionStorage.getItem(e[0]),e[0])}else{var n=new XMLHttpRequest;n.open("GET",e[1],true);r(n,"load",function(){if(n.readyState===4){u(n.responseText,e[0]);sessionStorage.setItem(e[0],n.responseText)}});n.send()}}}function u(e,t){if(document.getElementById(t)){document.getElementById(t).innerHTML=e}else{var n=document.createElement("style");n.innerHTML=e;document.getElementsByTagName("head")[0].appendChild(n)}}var e=document.getElementsByClassName("js-style");for(var n=0;n<e.length;n++){if(e[n].getAttribute("data-font")){css_href_array.push([e[n].getAttribute("id"),e[n].getAttribute("data-href")+t()+".css"])}else{css_href_array.push([e[n].getAttribute("id"),e[n].getAttribute("data-href")])}}for(var i in css_href_array){if(window.sessionStorage&&sessionStorage.getItem(css_href_array[i][0])||document.cookie.indexOf(css_href_array[i][0])>-1){o(css_href_array[i])}else{r(window,"load",o(css_href_array[i]))}}})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment