Skip to content

[labs/ssr] expose minimal CSSStyleSheet class in SSR DOM Shim #4862

Description

@thescientist13

Should this be an RFC?

  • This is not a substantial change

Which package is this a feature request for?

SSR (@lit-labs/ssr)

Description

Coming out of discussions in #2631 (comment) and from the recent Lit engineering call, the topic of splitting the scope off for just providing a DOM Shim for CSSStyleSheet (distinct from handling how to actually load a CSS file in a non-browser runtime) was proposed. This would allow for the usage of CSS Module Scripts with custom elements when server-rendering.

You can see the current behavior in this reproduction repo

/* greeting.css */
p { 
  color: blue;
}
// greeting.js
import { html, css, LitElement } from 'lit';
import sheet from './greeting.css' with { type: 'css' }

export class SimpleGreeting extends LitElement {
  static styles = [ sheet ];

  // ...
}

customElements.define('simple-greeting', SimpleGreeting);

And the output from running the build script

SSR Stylesheet detected...
file:///Users/owenbuckley/Workspace/github/lit-ssr-css-modules/components/greeting/greeting.css:2
        const sheet = new CSSStyleSheet();sheet.replaceSync(`p { 
                      ^

ReferenceError: CSSStyleSheet is not defined
    at file:///Users/owenbuckley/Workspace/github/lit-ssr-css-modules/components/greeting/greeting.css:2:23
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:473:24)
    at async file:///Users/owenbuckley/Workspace/github/lit-ssr-css-modules/register.js:5:1

The proposal for a DOM Shim itself would effectively be to stub out no-ops for the main methods of CSSStyleSheet, so as to at least not fail in an SSR context. (more work to improve their capabilities could be made over time if so desired)

class CSSStyleSheet {
  insertRule() { }
  deleteRule() { }
  replace() { }
  replaceSync() { }
}

Alternatives and Workarounds

I thought I could just shim this in from first party code or at least try and patch it through _node_modules, but it still produced the same error, so I assume it has something to do with how Lit+SSR is registering / shielding globals?

class CSSStyleSheet {
  insertRule() { }
  deleteRule() { }
  replace() { }
  replaceSync() { }
}

globalThis.CSSStyleSheet = globalThis.CSSStyleSheet ?? CSSStyleSheet;

Interestingly, I saw that CSSStyleSheet was defined in the DOM Shim, yet we still get CSSStyleSheet is undefined? 🤔

Activity

  1. kyubisation commented on Dec 20, 2024

    @kyubisation
    Contributor

    Hello @thescientist13
    As discussed yesterday, I'll take a shot at this.
    From my understanding, this includes:

    • CSSStyleSheet shim (and StyleSheet shim), with implementations for replaceSync and replace
    • CSSRule shim (as Lit seems to use this in packages/reactive-element/src/css-tag.ts, but I will skip all sub-classes of CSSRule)
    • A loader for import sheet from './greeting.css' with { type: 'css' }, which could be registered when the shim module is loaded

    As for the current CSSStyleSheet shim; It is not automatically added to the global context. You would need to import and apply getWindow from packages/labs/ssr/src/lib/dom-shim.ts. I do not know why this is currently the case and we can try to add this shim implementation to the global context and get feedback whether that is acceptable or not.

  2. kyubisation commented on Dec 20, 2024

    @kyubisation
    Contributor

    @thescientist13 I created #4879. Can you take a look and tell me whether this fits your need or your general impression?

  3. thescientist13 commented on Dec 20, 2024

    @thescientist13
    ContributorAuthor

    Awesome, thanks so much @kyubisation ! 🥳

    As for the current CSSStyleSheet shim; It is not automatically added to the global context. You would need to import and apply getWindow from packages/labs/ssr/src/lib/dom-shim.ts. I do not know why this is currently the case and we can try to add this shim implementation to the global context and get feedback whether that is acceptable or not.

    Yeah, that's interesting, since so far with my experience with Lit+SSR, I've been able to get away with just using the SSR render function as I understand the DOM shim to be loaded automatically when running on Node, so haven't needed to use getWindow (yet)
    https://lit.dev/docs/ssr/server-usage/#running-ssr-in-a-vm-module-or-the-global-scope

    As such, when Lit runs in Node, it automatically uses a set of minimal DOM APIs necessary to render Lit on the server, and defines the customElements global. (For a list of emulated APIs, see DOM emulation)

    I was thinking that this feature would / could be supported automatically like the rest of those APIs covered in the DOM emulation docs. 🤞


    PR looks great, btw!

  4. changed the title [-]expose minimal `CSSStyleSheet` class in SSR DOM Shim[/-] [+][labs/ssr] expose minimal `CSSStyleSheet` class in SSR DOM Shim[/+] on Jan 20, 2025
  5. moved this to 📋 Triaged in Lit Project Boardon Jan 20, 2025
  6. added
    Contributions Welcome
    on Jan 20, 2025
  7. moved this from 📋 Triaged to ✅ Done in Lit Project Boardon Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Contributions Welcome

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions