Fix for #2384 - fakes "sync" loading of web page by hiding it until styles are done

/ The actual XHR requests are changed to async for a speed boost for "sync" loading of orders of magnitude
This commit is contained in:
Matthew Dean
2015-11-25 22:52:24 -08:00
parent 73e79b0b84
commit c5283bb8d1
2 changed files with 23 additions and 3 deletions

View File

@@ -3,7 +3,7 @@
* used in the browser distributed version of less
* to kick-start less using the browser api
*/
/*global window */
/*global window, document */
// shim Promise if required
require('promise/polyfill.js');
@@ -15,11 +15,31 @@ var less = module.exports = require("./index")(window, options);
window.less = less;
var css, head, style;
if (options.onReady) {
if (/!watch/.test(window.location.hash)) {
less.watch();
}
// Simulate synchronous stylesheet loading by blocking page rendering
if (!options.async) {
css = 'body { display: none !important }';
head = document.head || document.getElementsByTagName('head')[0];
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
less.registerStylesheetsImmediately();
less.pageLoadFinished = less.refresh(less.env === 'development');
less.pageLoadFinished = less.refresh(less.env === 'development').then(function() {
if (!options.async) {
head.removeChild(style);
}
});
}

View File

@@ -39,7 +39,7 @@ module.exports = function(options, logger) {
FileManager.prototype.doXHR = function doXHR(url, type, callback, errback) {
var xhr = getXMLHttpRequest();
var async = options.isFileProtocol ? options.fileAsync : options.async;
var async = options.isFileProtocol ? options.fileAsync : true;
if (typeof xhr.overrideMimeType === 'function') {
xhr.overrideMimeType('text/css');