Skip to content

Commit

Permalink
Prevent another type of flash of unstyled content (#501)
Browse files Browse the repository at this point in the history
Handles the case of a site is configured with a fully-qualified URL as a
static asset prefix.
  • Loading branch information
gigabo authored Aug 10, 2016
1 parent cc44f9c commit 9c039a1
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/react-server/core/util/ClientCssHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@ module.exports = {
if (SERVER_SIDE) {
throw new Error("ClientCssHelper.registerPageLoad can't be called server-side");
}
const urlBase = location.protocol + "//" + location.host;

// for each css node in the head that the react-server server wrote to the response, note it down in the cache, so that
// we can remove it on a page to page transition.
var serverWrittenLinkNodes = document.head.querySelectorAll(`link[${PAGE_CSS_NODE_ID}],style[${PAGE_CSS_NODE_ID}]`);
for (var i = 0; i < serverWrittenLinkNodes.length; i++) {
var key, styleNode = serverWrittenLinkNodes[i];
if (styleNode.href) {
key = styleNode.href;

// The browser will give us a full URL even if we only put a
// path in on the server. So, if we're comparing against just
// a path here we need to strip the base off to avoid a flash
// of unstyled content.
if (key.indexOf(urlBase) === 0) {
key = key.substr(urlBase.length);
}
key = normalizeLocalUrl(styleNode.href);
} else {
key = styleNode.innerHTML;
}
Expand Down Expand Up @@ -100,6 +91,20 @@ module.exports = {
},

_keyFromStyleSheet: function(style) {
return style.href || style.text;
return normalizeLocalUrl(style.href) || style.text;
},
}

function normalizeLocalUrl(url) {
const urlBase = location.protocol + "//" + location.host;

// The browser will give us a full URL even if we only put a
// path in on the server. So, if we're comparing against just
// a path here we need to strip the base off to avoid a flash
// of unstyled content.
if (url && url.indexOf(urlBase) === 0) {
url = url.substr(urlBase.length);
}

return url;
}

0 comments on commit 9c039a1

Please sign in to comment.