Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix synchronously loading/applying stylesheets on page load. #2575

Merged
merged 1 commit into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/less-browser/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ require("./add-default-options")(window, options);

var less = module.exports = require("./index")(window, options);

window.less = less;

if (options.onReady) {
if (/!watch/.test(window.location.hash)) {
less.watch();
}

less.pageLoadFinished = less.registerStylesheets().then(
function () {
return less.refresh(less.env === 'development');
}
);
}
less.registerStylesheetsImmediately();
less.pageLoadFinished = less.refresh(less.env === 'development');
}
30 changes: 19 additions & 11 deletions lib/less-browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,28 @@ module.exports = function(window, options) {
less.unwatch = function () {clearInterval(less.watchTimer); this.watchMode = false; return false; };

//
// Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
// Synchronously get all <link> tags with the 'rel' attribute set to
// "stylesheet/less".
//
less.registerStylesheets = function() {
return new Promise(function(resolve, reject) {
var links = document.getElementsByTagName('link');
less.sheets = [];

for (var i = 0; i < links.length; i++) {
if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
(links[i].type.match(typePattern)))) {
less.sheets.push(links[i]);
}
less.registerStylesheetsImmediately = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't call less.registerStylesheetsImmediately from anywhere? and you have removed the call to less.registerStylesheets and that isn't called anywhere..

I guess you just need to call less.registerStylesheetsImmediately from bootstrap ?

otherwise, many thanks for tracking it down and fixing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the GitHub bug I mentioned in issue #2317. The line is actually there, right above the less.refresh call and promise assignment in bootstrap.js. However, GitHub appears to have a bug in the diff viewer where a delete line at the end of a file + adding a missing trailing newline to the file + adding a line results in that line getting skipped. I've already let GitHub know about it.

This is what the diff looks like for the change:

--- a/lib/less-browser/bootstrap.js
+++ b/lib/less-browser/bootstrap.js
@@ -13,14 +13,13 @@ require("./add-default-options")(window, options);

 var less = module.exports = require("./index")(window, options);

+window.less = less;
+
 if (options.onReady) {
     if (/!watch/.test(window.location.hash)) {
         less.watch();
     }

-    less.pageLoadFinished = less.registerStylesheets().then(
-        function () {
-            return less.refresh(less.env === 'development');
-        }
-    );
-}
\ No newline at end of file
+    less.registerStylesheetsImmediately();
+    less.pageLoadFinished = less.refresh(less.env === 'development');
+}

If you clone my tree and git show the commit, you'll see the line is there.

Also, would you mind trying to restart the Travis-CI build? They had some problems last night with random stalls, which was randomly affecting different builds at different points during the build process, triggering failures of 3 different types that I had noticed. Hoping that was some temporary issue on their end related to their failed upgrade and rollbacks.

var links = document.getElementsByTagName('link');
less.sheets = [];

for (var i = 0; i < links.length; i++) {
if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
(links[i].type.match(typePattern)))) {
less.sheets.push(links[i]);
}
}
};

//
// Asynchronously get all <link> tags with the 'rel' attribute set to
// "stylesheet/less", returning a Promise.
//
less.registerStylesheets = function() {
return new Promise(function(resolve, reject) {
less.registerStylesheetsImmediately();
resolve();
});
};
Expand Down