forked from morris/typekit-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove outer try/catch, save a few bytes
- Loading branch information
Showing
2 changed files
with
43 additions
and
51 deletions.
There are no files selected for viewing
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
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' ); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.