Skip to content

Commit

Permalink
Wait for CSS to load on client transition (#330)
Browse files Browse the repository at this point in the history
Don't start writing content into the DOM until external CSS has loaded.
  • Loading branch information
gigabo authored Jul 1, 2016
1 parent 109c900 commit 0e3bb5c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/react-server/core/util/ClientCssHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ module.exports = {
});

// next add the style URLs that weren't already loaded.
Object.keys(newCssByKey).forEach(newCssKey => {
return Q.all(Object.keys(newCssByKey).map(newCssKey => {
var retval;
if (!loadedCss[newCssKey]) {
// this means that the CSS is not currently present in the
// document, so we need to add it.
Expand All @@ -63,6 +64,14 @@ module.exports = {
styleTag = document.createElement('link');
styleTag.rel = 'stylesheet';
styleTag.href = style.href;

// If we _can_ wait for the CSS to be loaded before
// proceeding, let's do so.
if ('onload' in styleTag) {
var dfd = Q.defer();
styleTag.onload = dfd.resolve;
retval = dfd.promise;
}
} else {
styleTag = document.createElement('style');
styleTag.innerHTML = style.text;
Expand All @@ -75,7 +84,8 @@ module.exports = {
} else {
logger.debug(`Stylesheet already loaded (no-op): ${newCssKey}`);
}
});
return retval;
}));
});
},

Expand Down

0 comments on commit 0e3bb5c

Please sign in to comment.