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

[Declarative Shadow DOM Style Sharing] locator instead of specifier? #887

Open
jsebrech opened this issue Oct 7, 2024 · 1 comment
Open

Comments

@jsebrech
Copy link

jsebrech commented Oct 7, 2024

The proposal uses CSS module specifiers as a mechanism to identify CSS modules which are inlined into the page and which can then be referred to from the adoptedstylesheets attribute. This mechanism only works by using both attributes in combination.

Instead of exporting the module to the page as a CSS module specifier, what if it was exported using a locator (URL)?

<script type="css-module" locator="/foo.css"> 
  #content { 
    color: red; 
  } 
</script>

This would then make this CSS module exist as if it was loaded from the locator (URL). In other words, it inlines a networked resource and announces the inlined resource to the page by its locator. The resource would behave as if it was loaded with a GET with response header Cache-Control: no-store, not affecting the browser's cache, and would only exist for the lifetime of the document. Any reference to that URL would load the CSS module that was provided inline:

<my-element>  
  <template shadowrootmode="open">  
     <link rel="stylesheet" href="/foo.css" />
     ...
  </template>  
</my-element>

Benefits:

  • Falls back to fetching the resource from the network in non-supporting browsers, allowing for faster adoption.
  • No need for the adoptedstylesheets attribute, link tags or @import can use this without blocking or FOUC because the resource is immediately available in the page without an additional network request.
  • Can be used anywhere that the stylesheet is referenced, more flexible.
  • Can still be combined with the adoptedstylesheets attribute if desired, or with loading CSS modules in JS.
  • Could develop into a generic mechanism for inlining external resources for other resource types.
  • In supporting browsers a resource does not actually have to be available on that network URL.

Downsides:

  • Larger impact on the page's networking behavior.
  • Harder to polyfill. Basically the only strategy would be a service worker, and this is likely not a perfect polyfill.
@niutech
Copy link

niutech commented Oct 7, 2024

I think putting bare CSS rules inside <script> is very confusing for developers as well as IDEs providing syntax highlighting. I'd suggest instead putting inline CSS styles inside <style id="xxx"> and referencing them using <link rel="stylesheet" href="#xxx"> inside <template shadowrootmode="open">.

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

No branches or pull requests

2 participants