Skip to content

[labs/ssr-dom-shim] Register CSSStyleSheet polyfill in global scope when using Node.js hook for importing CSS files - #5240

Merged
kyubisation merged 2 commits into
lit:mainfrom
kyubisation:fix-ssr-dom-shim-css-hook
Mar 12, 2026
Merged

kyubisation merged 2 commits into
lit:mainfrom
kyubisation:fix-ssr-dom-shim-css-hook

Conversation

@kyubisation

@kyubisation kyubisation commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Currently if the DOM shim is not installed (or not installed before the import context of any import to lit or @lit/reactive-element), the conversion of CSSStyleSheet is hard locked into resulting in undefined, which is skipped.
Due to this, during the registering of the CSS import hook, the CSSStyleSheet is added to the global scope.
Additionally, this changes the native CSS import feature detection to use a data url to avoid bundlers failing when inlining the CSS file or skipping it entirely.

@changeset-bot

changeset-bot Bot commented Feb 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c11b169

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@lit-labs/ssr-dom-shim Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@thescientist13 thescientist13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @kyubisation 🙌

@kyubisation

Copy link
Copy Markdown
Contributor Author

We discussed this during the community call:

  • There was opposition to changing the client side code in [packages/reactive-element/src/css-tag.ts‎. The preference is in just adapting the Node.js code path.
  • The situation that there can be an instance of CSSStyleSheet without it being in the global scope is seen as unfortunate. Some options were discussed:
  1. The CSS hook for Node.js removes the import to the CSSStyleSheet DOM shim and warns/throws if CSSStyleSheet is not in the global scope (i.e. requires using the DOM shim or similar, at least for CSSStyleSheet)
  2. The CSS hook registers CSSStyleSheet on hook registration in the global scope
  3. Keep it as it is and just adapt css-tag.ts

@thescientist13 What is your use case/opinion? Personally I'm inclined to 1., but I'd like your input on this.

@thescientist13

thescientist13 commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

@kyubisation
Not sure if I am 100% following along but it sounds like it mostly a concern around CSSStyleSheet leaking into the global scope?

Regarding option 1:

The CSS hook for Node.js removes the import to the CSSStyleSheet DOM shim and warns/throws if CSSStyleSheet is not in the global scope (i.e. requires using the DOM shim or similar, at least for CSSStyleSheet)

Are you saying that for these example here, I would just have manually add an import for the DOM shim script specifically, as opposed to Lit SSR just automatically loading it in a NodeJS environment?

Regarding option 3:

Keep it as it is and just adapt css-tag.ts

What would adapting in this case look like?


Not sure if this is useful info at, but for my use case, I am (already) using Worker Threads to do SSR, mainly to isolate SSR code like this from the global scope, so not sure how that would factor into your decision making?

That said, I've been considering moving away from worker threads just because when using --import from the CLI to run Node, all these hooks get registered over and over for every worker thread, as opposed to shared, which i guess is by design. (NodeJS thread for reference - nodejs/loaders#201 (comment))

That said, I do need something like Worker Threads for local development / live reloading to do module cache busting, since ESM has no module invalidation API like CJS did - nodejs/node#49442

Maybe the VM approach Lit recommends would be my way to go? Or I was thinking of using Cluster, but ideally I'm trying to find something (at least long term) that would be "web standard" / WinterTC compatible. Maybe that's Shadow Realms? 🤔


All that yapping to say, and apologies if I didn't actually answer / get your question, is that I'm pretty much not opposed to any particular option in general, though I wouldn't be opposed to #2 since I always love it when a library does the thing for me 😅 ((though I guess there is still the case of Lit wanting to future proof in the event Node actually supports CSSStyleSheet in the future?)

@kyubisation

Copy link
Copy Markdown
Contributor Author

@thescientist13 Thank you for your input. After some considerations I think I agree with you of making it "just work" with option 2. I'll implement a corresponding approach and update the PR. 👍

@kyubisation
kyubisation force-pushed the fix-ssr-dom-shim-css-hook branch 2 times, most recently from 318e49e to 607604f Compare March 1, 2026 17:42
@kyubisation kyubisation changed the title [labs/ssr-dom-shim] Fix the conversion of CSSStyleSheet in SSR when the DOM shim is not installed [labs/ssr-dom-shim] Register CSSStyleSheet polyfill in global scope when using Node.js hook for importing CSS files Mar 1, 2026
@kyubisation

Copy link
Copy Markdown
Contributor Author

@thescientist13 Also in #5153 I'm experimenting with Lit Worker support for Node.js. If you have the time to take a look at that and provide your input, it should help in shaping it for usability.

@justinfagnani justinfagnani left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@kyubisation
kyubisation force-pushed the fix-ssr-dom-shim-css-hook branch from 607604f to c11b169 Compare March 12, 2026 18:02
@kyubisation
kyubisation enabled auto-merge (squash) March 12, 2026 19:15
@kyubisation
kyubisation merged commit b9de429 into lit:main Mar 12, 2026
10 of 11 checks passed
@kyubisation
kyubisation deleted the fix-ssr-dom-shim-css-hook branch March 12, 2026 19:44
kyubisation added a commit to kyubisation/lit that referenced this pull request May 7, 2026
…hen using Node.js hook for importing CSS files (lit#5240)

Currently if the DOM shim is not installed (or not installed before the import context of any import to `lit` or `@lit/reactive-element`), the conversion of `CSSStyleSheet` is hard locked into resulting in undefined, which is skipped.
Due to this, during the registering of the CSS import hook, the `CSSStyleSheet` is added to the global scope.
Additionally, this changes the native CSS import feature detection to use a data url to avoid bundlers failing when inlining the CSS file or skipping it entirely.
@lit-robot lit-robot mentioned this pull request May 14, 2026
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

Successfully merging this pull request may close these issues.

3 participants