You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a module is imported imperatively in this fashion and the Declarative CSS Module is not in the [module map](https://html.spec.whatwg.org/#module-map), the import fails, even if it is added declaratively at a later time.
482
483
483
-
## Other declarative modules
484
+
### Static Versus Dynamic Values For shadowrootadoptedstylesheets Specifiers
485
+
486
+
Stylesheets referenced by specifiers in the `shadowrootadoptedstylesheets` attribute are static - they are only applied if they are available when their associated `<template>` tag is parsed.
487
+
488
+
In the following example, the stylesheet referenced by the specifier "foo" is not applied to the adopted stylesheets array because the declarative
489
+
definition of "foo" is after the `<template>` tag:
Similarly, for external files, no styles are adopted into the `<template>` because "foo.css" hasn't been loaded into the module map at the time the `<template>` tag has been parsed:
This could be addressed by treating the specifier as a dynamic reference and invalidating styles accordingly when that reference changes. However, supporting this scenario is not ideal for several reasons:
516
+
* It would introduce a dependency on the module map to track all missing references and respond to updates. This is not ideal because it adds an additional performance cost for module map updates.
517
+
* There is no way to support this scenario without causing a FOUC. FOUC should be avoided because it is a poor user experience and causes extra work for the renderer.
518
+
* Once created, specifiers cannot change. Introducing new side effects when specifiers are created but not changed could be considered ununititive.
519
+
* This approach more closely aligns with the imperative `adoptedStyleSheets` API, as stylesheets must exist before they can be added to the `adoptedStyleSheets` array.
520
+
521
+
For these reasons, we do not believe it is worth persuing making specifiers dynamic.
522
+
523
+
### Why shadowrootadoptedstylesheets Does Not Perform a Fetch
524
+
525
+
The current design does not fetch the specifiers listed in `shadowrootadoptedstylesheets` - they must be present in the module map at the time `shadowrootadoptedstylesheets` is parsed.
526
+
527
+
There are several reaons for this behavior:
528
+
* Fetching stylesheets at parse time would introduce a FOUC for the initial stylesheet fetch, with additional style invalidations upon each subsequent fetch complete. FOUC should be avoided because it is a poor user experience and causes extra work for the renderer.
529
+
* A URL alone does not allow for any flexibility of the fetch. Properties such as [`fetchpriority`](https://html.spec.whatwg.org/#dom-link-fetchpriority) and [`blocking`](https://html.spec.whatwg.org/#attr-style-blocking) that `<link rel="modulepreload">` supports are not possible to apply with a URL alone.
530
+
* There is already a mechanism for declaratively fetching modules with `<link rel="modulepreload">`.
531
+
* There is already a mechanism for loading external CSS files in Shadow Roots via `<link rel="stylesheet">`.
532
+
* The imperative `adoptedStyleSheets` does not perform a fetch, so not fetching is consistent with that version.
533
+
534
+
For these reasons, we do not believe that it is necessary to perform a fetch for each entry in `shadowrootadoptedstylesheets`.
535
+
536
+
## Other Declarative Modules
484
537
An advantage of this approach is that it can be extended to solve similar issues with other content types. Consider the case of a declarative component with many instances stamped out on the page. In the same way that the CSS must either be duplicated in the markup of each component instance or set up using script, the same problem applies to the HTML content of each component. We can envision an inline version of [HTML module scripts](https://github.com/WICG/webcomponents/blob/gh-pages/proposals/html-modules-explainer.md) that would be declared once and applied to any number of shadow root instances:
0 commit comments