-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Proposal: a non-render-blocking way to load stylesheets #3672
Comments
Existing solutionsA. Element added with JavaScriptSomething like: document.addEventListener('DOMContentLoaded', () => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '...';
document.head.appendChild(link);
}) Pros:
Cons:
B. Non-matching
|
I’m personally in favor of solution A. Also, to explain the “it’s a hack” cons. These hacks might look OK for folks with web background (including myself), but, in my experience, they’re a part of “death by thousand cuts” for non-web developers. That’s why I prioritize this. It’s not cool to have conversations like “How do I do this [basic stuff]?” – “Oh, there’s no proper solution for this, use one of these hacks depending on your requirements” – “Uh, okay.” |
You may add link tag with ref preload to the top and link tag with ref stylesheet to the bottom, without async attribute. |
I am confused by this, this already exists years ago https://www.filamentgroup.com/lab/async-css.html |
@montogeek main idea — don't use js and use only one styles definition. Actually idea is quite good. |
Also http2 push kinda solve this problem too. Almost. |
One another question: |
Just tested with Chrome stable, and this didn’t work. Do you have a reproducible example?
Server push would solve the preloading issue – but AFAIK the browser won’t apply the pushed stylesheet without the
Great point, I missed this! |
Right now: …content…
<link rel="stylesheet" href="…"><script> </script> "content" can render before the stylesheet loads in every browser except Chrome (ticket). This means you can achieve async stylesheet loading using a preload plus a stylesheet at the bottom of the document. You can also incrementally load styles by placing stylesheets just before the first element that needs them. The above is more versatile, and it'd be nice if it was standardised (along with a change to Firefox so the An |
In general I agree with @jakearchibald in that this is a decent idea but might not be most useful to incrementally apply styles. I'm wondering if a lot of the questions that might be brought up by this thread regarding the
FWIW, under existing solutions, (B) doesn't always work; for example, see #2886, which Firefox has just about solved but it is not in stable yet. Regarding (C), I believe @wanderview told me that Firefox's preload implementation is currently turned off, so I don't think that'll load. And regarding the priority associated with preloads, the HTML Standard mentions that the priority should be the same as the |
Thanks for the feedback! Yup, rendering the page until a Closing this in favor of #1349 as it covers standardization of this |
Based on my understanding #1349 only applies to |
We discussed maybe expanding the /cc @xiaochengh |
Proposal: let’s have something like
for loading and applying stylesheets without blocking the page rendering.
User story
I, as an application developer who cares about performance, want to have a non-render-blocking way to load non-critical CSS.
Critical CSS is the part of site styles that are required for the very first rendering. This could be e.g. CSS for above-the-fold content. Non-critical CSS is the remaining styles.
Specific use cases
In a news site: load and apply styles for the page layout and the news content + hide the comments/ads blocks. Then, without blocking the initial rendering, load and apply styles for comments and ads.
On a landing page: load and apply styles for above-the-fold content + hide the content below the fold. Then, without blocking the initial rendering, load and apply styles for below-the-fold stuff.
The text was updated successfully, but these errors were encountered: