Skip to content

Commit

Permalink
🐛 Fix bug in LoadCssAsync.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Oct 16, 2017
1 parent e4600cf commit 73b1c87
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 125 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/Build
/package.json
/yarn.lock
44 changes: 44 additions & 0 deletions Resources/Private/Assets/LoadCSSAsynchron/PreloadPolyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */
(function( w ){
// rel=preload support test
if( !w.loadCSS ){
return;
}
var rp = loadCSS.relpreload = {};
rp.support = function(){
try {
return w.document.createElement( "link" ).relList.supports( "preload" );
} catch (e) {
return false;
}
};

// loop preload links and fetch using loadCSS
rp.poly = function(){
var links = w.document.getElementsByTagName( "link" );
for( var i = 0; i < links.length; i++ ){
var link = links[ i ];
if( link.rel === "preload" && link.getAttribute( "as" ) === "style" ){
w.loadCSS( link.href, link, link.getAttribute( "media" ) );
link.rel = null;
}
}
};

// if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS
if( !rp.support() ){
rp.poly();
var run = w.setInterval( rp.poly, 300 );
if( w.addEventListener ){
w.addEventListener( "load", function(){
rp.poly();
w.clearInterval( run );
} );
}
if( w.attachEvent ){
w.attachEvent( "onload", function(){
w.clearInterval( run );
} );
}
}
}( window ));
74 changes: 74 additions & 0 deletions Resources/Private/Assets/LoadCSSAsynchron/loadCSS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
(function(w){
"use strict";
/* exported loadCSS */
var loadCSS = function( href, before, media ){
// Arguments explained:
// `href` [REQUIRED] is the URL for your CSS file.
// `before` [OPTIONAL] is the element the script should use as a reference for injecting our stylesheet <link> before
// By default, loadCSS attempts to inject the link after the last stylesheet or script in the DOM. However, you might desire a more specific location in your document.
// `media` [OPTIONAL] is the media type or query of the stylesheet. By default it will be 'all'
var doc = w.document;
var ss = doc.createElement( "link" );
var ref;
if( before ){
ref = before;
}
else {
var refs = ( doc.body || doc.getElementsByTagName( "head" )[ 0 ] ).childNodes;
ref = refs[ refs.length - 1];
}

var sheets = doc.styleSheets;
ss.rel = "stylesheet";
ss.href = href;
// temporarily set media to something inapplicable to ensure it'll fetch without blocking render
ss.media = "only x";

// wait until body is defined before injecting link. This ensures a non-blocking load in IE11.
function ready( cb ){
if( doc.body ){
return cb();
}
setTimeout(function(){
ready( cb );
});
}
// Inject link
// Note: the ternary preserves the existing behavior of "before" argument, but we could choose to change the argument to "after" in a later release and standardize on ref.nextSibling for all refs
// Note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/
ready( function(){
ref.parentNode.insertBefore( ss, ( before ? ref : ref.nextSibling ) );
});
// A method (exposed on return object for external use) that mimics onload by polling document.styleSheets until it includes the new sheet.
var onloadcssdefined = function( cb ){
var resolvedHref = ss.href;
var i = sheets.length;
while( i-- ){
if( sheets[ i ].href === resolvedHref ){
return cb();
}
}
setTimeout(function() {
onloadcssdefined( cb );
});
};

function loadCB(){
if( ss.addEventListener ){
ss.removeEventListener( "load", loadCB );
}
ss.media = media || "all";
}

// once loaded, set link's media back to `all` so that the stylesheet applies once it loads
if( ss.addEventListener ){
ss.addEventListener( "load", loadCB);
}
ss.onloadcssdefined = onloadcssdefined;
onloadcssdefined( loadCB );
return ss;
};

w.loadCSS = loadCSS;
}( window ));
File renamed without changes.
45 changes: 0 additions & 45 deletions Resources/Private/Components/LoadCSSAsynchron/PreloadPolyfill.js

This file was deleted.

77 changes: 0 additions & 77 deletions Resources/Private/Components/LoadCSSAsynchron/loadCSS.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
prototype(Carbon.IncludeAssets:LoadJSforCSSAsynchron) < prototype(Carbon.IncludeAssets:FileScript) {
file = 'LoadCssAsync'
file = 'LoadCssAsync[async]'
assetResourceFolder = 'Carbon.IncludeAssets/Public'

config = ${Configuration.setting('Carbon.IncludeAssets')}
Expand Down
6 changes: 4 additions & 2 deletions Resources/Public/LoadCssAsync.js

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

1 change: 1 addition & 0 deletions Resources/Public/LoadCssAsync.js.map

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

29 changes: 29 additions & 0 deletions gulp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"info": {
"description": "Carbon.IncludeAssets",
"author": "Jon Uhlmann",
"homepage": "https://github.com/jonnitto/Carbon.IncludeAssets"
},
"root": {
"base": "./Resources",
"src": "Private",
"dest": "Public"
},
"browserSync": {
"enable": false
},
"tasks": {
"scssLint": false,
"jsLint": false,
"fonts": false,
"images": false,
"static": false,
"svgSprite": false,
"js": {
"dest": ""
},
"css": {
"dest": ""
}
}
}

0 comments on commit 73b1c87

Please sign in to comment.