Comments (10)
Hey @paulirish
Thanks for the detailed writeup. Sounds reasonable to me for the default. That said, we've been recommending the use of the before
argument in loadCSS
to specify a particular element to insert the CSS before, and we recommend passing in the script
element that holds the loadCSS function as one easy way to do that (since it's in a dependable place in the head
.
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
from loadcss.
So I'd go for something like this:
var elems = document.querySelectorAll('style,link[rel=stylesheet]');
var ref = elems.item(elems.length - 1);
ref.parentNode.insertBefore(ss, ref.nextSibling);
It finds all external stylesheets and inline styles, gets the last one (in DOM order), then adds our ss right after it.
Not having any other style-definitions on a page beside the web-font we try to load, this code would fail.
from loadcss.
Fair point, @whjvenyl, even if that's a rare case.
Including script
in that lookup should cover most cases I'd think.
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 before
argument and letting folks define their insertion point specifically. This can be done pretty easily by adding an ID to the script
element that contains loadCSS, as our docs show, or by adding a meta tag or some other element as a placeholder reference for insertion.
Thoughts on requiring that arg instead of making the script cleverer, @paulirish ?
from loadcss.
Okay, @paulirish : how's this PR look?
#87
from loadcss.
hey scott, thanks.
I think we could do slightly better. I'm concerned that just requiring before
won't reduce the chance that browser styles are recalculated. Plus requiring the insertion point kinda sucks for users.
How about...
- attempt to place after last stylesheet.
- If no results or no qSA, fall back to before last script.
- if
before
provided, place before given element
from loadcss.
@paulirish sounds reasonable, and I like the idea of keeping the 2nd arg optional. Cool, cool.
from loadcss.
@paulirish any pro/con to the item()
method in your example over say, []
reference syntax? That one's actually new to me (thanks! :) )
from loadcss.
@paulirish I was looking to pack this up for 0.2
and had a quick question. The QSA check is a small bummer to me because it leaves the potential for a busted CSS cascade in non-qsa browsers, unless the before
argument is there to specify a specific placement.
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 before
is still needed with the current code.
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.
var scope = document.body || document.getElementsByTagName( "head" )[ 0 ];
var lastElement = scope.childNodes[ scope.childNodes.length - 1 ];
Alternatively, a lookup like document.getElementsByTagName('*');
could get us to the last element node too (but I dunno if that lookup is slow or perhaps aliased internally).
Anyway, have any thoughts on the snippet above in place of the qsa usage?
from loadcss.
new related issue #100
from loadcss.
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
from loadcss.
Related Issues (20)
- FOUC on 'No Throttled' condition? HOT 2
- Polyfill has an issue in chrome
- Optionally preload without as attr?
- Don't rely on invalid media attributes to download? HOT 10
- standalone or via npm?
- element with id `global` prevents loadCSS execution
- Error thrown by Content-Security-Policy HOT 5
- Supports preload
- Cannot use inline 'onload' because of CSP HOT 6
- The plugin doesn't do anything to preload elements in Firefox HOT 1
- Cannot install version 3.0.0 because node version is hardcoded to 11.9.0 HOT 5
- Where has the cssrelpreload polyfill gone in 3.0.0? HOT 2
- Required node version HOT 5
- Export onloadCSS
- CSS is being loaded twice HOT 3
- loadCSS not helping with render-blocking content
- Why do I sometimes get a recommendation in google pagespeed 'Reduce unused CSS' ? HOT 1
- Wouldn't loading css asynchronously cause FOUC? HOT 1
- Calling loadCSS from a TypeScript Vite application throws Uncaught TypeError: Cannot read properties of undefined (reading 'createElement') at loadCSS (loadCSS.js:13:16)
- Help - loadCSS/Critical file load not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loadcss.