-
Notifications
You must be signed in to change notification settings - Fork 534
Performance of invalidating all stylesheets #85
Comments
Hey @paulirish Thanks for the detailed writeup. Sounds reasonable to me for the default. That said, we've been recommending the use of the Here's an example of that usage: https://github.com/filamentgroup/loadCSS#optional-arguments It comes with the benefit of knowing that browser extensions can't make your inject location inconsistent too, which is nice. Given that that option is there and recommended, would you say we should keep that recommendation in place or just go with this new default you've recommended above? Thanks |
Not having any other style-definitions on a page beside the web-font we try to load, this code would fail. |
Fair point, @whjvenyl, even if that's a rare case. Including Speaking of rare cases, we'll want this script to continue working in browsers that don't have querySelector support too, so the actual lookup for this will be a little more verbose... Which leads me back to wondering if it might be best to simply start requiring the Thoughts on requiring that arg instead of making the script cleverer, @paulirish ? |
Okay, @paulirish : how's this PR look? |
hey scott, thanks. I think we could do slightly better. I'm concerned that just requiring How about...
|
@paulirish sounds reasonable, and I like the idea of keeping the 2nd arg optional. Cool, cool. |
@paulirish any pro/con to the |
…lesheet or script in the page if qsa is supported. Otherwise it inserts after the last script. In either case, the insertBefore is performed in the reference element's nextSibling, which serves as an "insertAfter" regardless of whether the reference element has a nextSibling or not. Despite the default behavior change, we've preserved the behavior of the "before" argument, if provided, so that the link is still inserted before that specific element in that case. We may want to standardize on inserting "after" in a later release. Fixes #85.
@paulirish I was looking to pack this up for Of course, non-qsa browsers are rarer now, but I think as a bare minimum, loadCSS should still try to preserve cascade order in a browser that doesn't have qsa, which means That got me thinking that maybe there is a non-qsa approach to what we're trying to do. For example, something like this could give us an acceptable node to append after... basically, the last node in the body or head.
Alternatively, a lookup like Anyway, have any thoughts on the snippet above in place of the qsa usage? |
new related issue #100 |
Okay @paulirish your change is in version 0.2.0, along with that newer fallback so that it'll inject after all scripts and styles even if qsa is not supported. thanks |
Background
Browsers are built to optimize appending new styles to the end. That's why when you apply an inline style, browsers don't have to recompute the styles of the world in order to apply it.
The model is built to cheaply apply additive style rules; the browser takes the current CSSOM (basically) and applies the new css on top of it. It's cool.
Because CSS order matters, if new CSS is added in the middle (let's say halfway down), then the browser invalidates the style computation of 50% of the styles and has to recompute due to the potential cascade changes.
Current state
loadCSS injects the script like so:
This injection is better than adding to the very top of the head, but there's a good chance it is before other styles.
In discussions with Flipkart, the performance disadvantage they encountered with this was significant. I would expect similar results amongst publishers with plenty of DOM, the folks that typically would be eager to use loadCSS.
Proposal
I'd recommend placing the injected sheet at the end of all recognized styles. (Bonus: subsequent loadCSS calls will be generally placed after eachother, which is probably what folks would expect)
So I'd go for something like this:
It finds all external stylesheets and inline styles, gets the last one (in DOM order), then adds our
ss
right after it.hows that sound?
The text was updated successfully, but these errors were encountered: