Repository navigation
Description
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:1The 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? 🤔
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields✅ Done
Activity
kyubisation commentedon Dec 20, 2024
Hello @thescientist13
As discussed yesterday, I'll take a shot at this.
From my understanding, this includes:
import sheet from './greeting.css' with { type: 'css' }, which could be registered when the shim module is loadedAs 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.
kyubisation commentedon Dec 20, 2024
@thescientist13 I created #4879. Can you take a look and tell me whether this fits your need or your general impression?
thescientist13 commentedon Dec 20, 2024
Awesome, thanks so much @kyubisation ! 🥳
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
renderfunction as I understand the DOM shim to be loaded automatically when running on Node, so haven't needed to usegetWindow(yet)https://lit.dev/docs/ssr/server-usage/#running-ssr-in-a-vm-module-or-the-global-scope
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!
[-]expose minimal `CSSStyleSheet` class in SSR DOM Shim[/-][+][labs/ssr] expose minimal `CSSStyleSheet` class in SSR DOM Shim[/+]