lwc icon indicating copy to clipboard operation
lwc copied to clipboard

SSR+hydration should not result in duplicated stylesheets across HTML and JS

Open nolanlawson opened this issue 1 year ago • 1 comments

Right now, the way SSR works is that renderComponent renders the component stylesheets as <style>s:

<x-foo>
  <template shadowrootmode=open>
    <style> div { color: red } </style>
    <div>Hello world</div>
  </template>
</x-foo>

And then this stylesheet is duplicated inside of the JS bundle as well:

function stylesheet(token, useActualHostSelector, useNativeDirPseudoclass) {
  var shadowSelector = token ? ("[" + token + "]") : "";
  return "div" + shadowSelector + " {color: red;}";
}

This is fairly wasteful to include the same content twice. Especially if disableSyntheticShadowSupport is set in the compiler, then the strings are always the same (at least for shadow DOM – light DOM is trickier because of :host and useActualHostSelector).

Ideally there should be some way to avoid this duplication entirely, i.e. to only put the stylesheet into the HTML, not the JS.

Related: https://github.com/salesforce/lwc/issues/2851

nolanlawson avatar Jan 11 '24 18:01 nolanlawson

It doesn't address this issue directly, but the introduction of <lwc-style> in #5264 can help mitigate increased bundle size.

wjhsf avatar Mar 28 '25 19:03 wjhsf