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

[Proposal] : Make stylesheets with non-matching media queries imported using "@import" non-render blocking. #8958

Open
yashjoshi-dotcom opened this issue Feb 27, 2023 · 2 comments

Comments

@yashjoshi-dotcom
Copy link

yashjoshi-dotcom commented Feb 27, 2023

Currently all major browser engines [Chromium, WebKit, Gecko] block rendering and script execution on @import imported stylesheets with a non matching MQ. As a result, they also load such scripts eagerly which leads to unreasonably degraded performance.

A min. reproducible example is presented here for reference. [wait 10s for site to load]
Explanation : The stylesheet is imported here from using @import with print media inside a parent stylesheet [which has matching media]. The imported stylesheet's MQ does not match with the device's media environment but still browser waits for it to fetch completely before continuing JS execution. Here the stylesheet is delayed by 10 sec which delays rendering and script execution ( Time Elapsed can be observed in console window ).

The interaction of scripting and styling part of the current HTML spec talks about the media element and how scripts should not be blocked when the media environment does not match. However, it does not have any mention regarding @import . We need to address this ambiguity, especially when conditional media rules clearly state that

"User agents may, therefore, avoid fetching a conditional import as long as the import conditions do not match.".

See more discussion @ Chromium Bug

The main challenge here for CSS spec change is that import rules can be mixed with various other style rules not covered by the media query, such as.

<style>
  @import url("delayed-stylesheet.css") print; // applied only when media environment is print
  body { background-color: lightblue; }  // applied in every case.
</style>

We don't seem to have the concept of a partially loaded stylesheet in CSSOM as of now, which seems to be the main blocker here.

One possible change to address this could mean that the CSSImportRule is present, but its styleSheet is initially null, which has been discussed here

I believe incorporating this change would improve the performance of web applications and make them seem more responsive to user interaction.

Thank you for your time and consideration.

@yoavweiss
Copy link
Collaborator

^^ @emilio @lilles @dbaron

@zcorpan
Copy link
Member

zcorpan commented Feb 28, 2023

cc @whatwg/css - as noted above this would need changes in CSSOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants