Skip to content

Commit

Permalink
remove outer try/catch, save a few bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
morris committed Mar 20, 2016
1 parent 19b846a commit 0c8e465
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
92 changes: 42 additions & 50 deletions typekit-cache.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,68 @@
try {
( function ( document, proto, method, storage, key, domain, /* min */ cached, style, setAttribute ) {

( function ( document, proto, storage, key, domain, /* min */ cached, style, setAttribute ) {
// If CSS is in cache, append it to <head> in a <style> tag.

// If CSS is in cache, append it to <head> in a <style> tag.
cached = storage[ key ];
if ( cached ) {
style = document.createElement( 'style' );
style.innerHTML = cached;
document.getElementsByTagName( 'head' )[ 0 ].appendChild( style );
document.documentElement.className += ' wf-cached';
}

cached = storage[ key ];
if ( cached ) {
style = document.createElement( 'style' );
style.innerHTML = cached;
document.getElementsByTagName( 'head' )[ 0 ].appendChild( style );
document.documentElement.className += ' wf-cached';
}

// The typekit will at some point create a <link> to load its CSS.
// Override Element.prototype.setAttribute to handle setting its href.

setAttribute = proto.setAttribute;
proto.setAttribute = function ( name, url, /* min */ xhr, css ) {

if ( typeof url === 'string' && url.indexOf( domain ) > -1 ) {
// The typekit will at some point create a <link> to load its CSS.
// Override Element.prototype.setAttribute to handle setting its href.

try {
setAttribute = proto[ method ];
proto[ method ] = function ( name, url, /* min */ xhr, css ) {

// Get the CSS of the URL via XHR and cache it.
// Only overwrite cache if CSS has changed.
if ( typeof url == 'string' && url.indexOf( domain ) > -1 ) {

xhr = new XMLHttpRequest();
xhr.open( 'GET', url, true );
xhr.onreadystatechange = function () {
try {

try {
// Get the CSS of the URL via XHR and cache it.
// Only overwrite cache if CSS has changed.

if ( xhr.readyState === 4 ) {
xhr = new XMLHttpRequest();
xhr.open( 'GET', url, true );
xhr.onreadystatechange = function () {

// Make relative URLs absolute. Fixes #2
css = xhr.responseText.replace( /url\(\//g, 'url(' + domain + '/' );
try {

// Store new CSS if modified.
if ( css !== cached ) storage[ key ] = css;
if ( xhr.readyState == 4 ) {

}
// Make relative URLs absolute. Fixes #2
css = xhr.responseText.replace( /url\(\//g, 'url(' + domain + '/' );

} catch ( ex ) {

// Fall back to regular behavior. Fixes #3
if ( style ) style.innerHTML = '';
// Store new CSS if modified.
if ( css !== cached ) storage[ key ] = css;

}

};
xhr.send( null );
} catch ( x ) {

} catch ( ex ) {
// Fall back to regular behavior. Fixes #3
if ( style ) style.innerHTML = '';

// The only possible side effect here is an empty <style> element.
}

}
};
xhr.send( null );

// Reset Element.prototype.setAttribute
proto.setAttribute = setAttribute;
} catch ( x ) {

}
// The only possible side effect here is an empty <style> element.

// Always apply original setAttribute
return setAttribute.apply( this, arguments );
}

};
// Reset Element.prototype.setAttribute
proto[ method ] = setAttribute;

} )( document, Element.prototype, localStorage, 'tk', 'https://use.typekit.net' );
}

} catch ( ex ) {
// Always apply original setAttribute
return setAttribute.apply( this, arguments );

// Don't care about empty <style> element.
};

}
} )( document, Element.prototype, 'setAttribute', localStorage, 'tk', 'https://use.typekit.net' );
2 changes: 1 addition & 1 deletion typekit-cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c8e465

Please sign in to comment.