-
-
Notifications
You must be signed in to change notification settings - Fork 473
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
Idea: Debounce adding of styles for better performance #5
Comments
What about this? https://github.com/wilsonpage/fastdom |
Interesting module. How could the fastdom instance be shared between the loader and the app? |
With fastdom: fastdom.write(function() {
require("./style.css");
}); Style added and removed only when component is used: var style = require("./style.usable.css"); // and bind "style/usable!css" to ".usable.css"
constructor {
style.use();
}
destructor {
process.nextTick(function() {
style.unuse();
});
} Nothing to do in this loader. |
Yep, you're right. This should be done on application level. But this is a nice pattern. Imho it should be documented in the readme. |
I'm not sure if webpack should solve this either, but how about the following situations?
It could be cool if These may be the responsibility of the framework; not sure. But is the first problem even solvable with the current API? |
Assuming a module that uses the reference-counted API like this: var style = require("./style.useable.css");
style.use();
style.unuse(); And the application uses the recommended configuration: {
module: {
loaders: [
{ test: /\.css$/, exclude: /\.useable\.css$/, loader: "style!css" },
{ test: /\.useable\.css$/, loader: "style/useable!css" }
]
}
} It's possible for the application to apply any additional strategy that does batching (etc.). Just prepend a loader that decorates the public API of the generated module: {
module: {
loaders: [
{ test: /\.css$/, exclude: /\.useable\.css$/, loader: "style!css" },
{ test: /\.useable\.css$/, loader: "batching-style-loader!style/useable!css" }
]
}
} // batching-style-loader.js
module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
return "module.exports = require(" +
JSON.stringify(path.join(__dirname, "batching-style-decorator.js")) +
")(require(" + JSON.stringify(remainingRequest) + "));";
}; // batching-style-decorator.js
var queue = [];
module.exports = function decorate(style) {
function ref() {
// Apply now
style.ref();
queue.forEach(function(style) {
style.unref();
});
}
function unref() {
process.nextTick(function() {
// Unapply on the next ref... or whatever
queue.push(style);
});
}
return {
ref: ref,
use: ref,
unref: unref,
unuse: unref
};
}; |
Use fastdom over process.nextTick to eliminate layout thrashing :) |
Have you looked into using the |
I don't know such a |
Link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style#attr-disabled (Toggling the |
I like that suggestion. Seems the Example: http://jsbin.com/reneca/2/edit Explicitly adding it as an attribute in the tag won't work; I think is non-standard:
|
Interesting... according to this answer |
yeah, we'd only need to use the DOM API |
It can be closed, but a note in the README would be good that the styles should be applied in batches when used in production (for instance via fastdom). |
I'm thinking about this issue quite a while but haven't found the time to write it down.
My co-worker was working on an app (without webpack) where he wanted to define style rules per component. These styles should only be applied if the component is currently in use, so he added and removed the style nodes accordingly.
While the theory is very nice, it has a serious performance impact. Because when a style node is added or removed the browser needs to recalculate all styles and to reflow the whole document. So when a page changed, the browser needed to recalculate and to reflow several times.
I was wondering if this problem could be solved by the style-loader. I was thinking about debouncing the call where the
<style>
node is actually added to the DOM.This feature needs to be tested carefully to avoid FOUTs (Flash of Unstyled Text) or other ugly problems when unstyled content is added to the DOM.
The text was updated successfully, but these errors were encountered: