Skip to content

[labs/ssr] LitElement renders content inside a Declarative Shadow DOM when using createRenderRoot server side #3080

@thescientist13

Description

@thescientist13

Description

When using Lit SSR and enabling createRenderRoot for a custom element, Lit is rendering the content inside declarative shadow DOM <template> tag.

Steps to Reproduce

  1. Given the sample Greeting component that uses createRenderRoot
    import { html, css, LitElement } from 'lit';
    
    export class SimpleGreeting extends LitElement {
      static styles = css`p { color: blue }`;
    
      static properties = {
        name: {type: String},
      };
    
      constructor() {
        super();
        this.name = 'Somebody';
      }
    
      createRenderRoot(){
        return this;
      }
    
      render() {
        return html`<p>Hello, ${this.name}!</p>`;
      }
    }
    
    customElements.define('simple-greeting', SimpleGreeting);
  2. Server Render the component
    import { render } from '@lit-labs/ssr/lib/render-with-global-dom-shim.js';
    import { html } from 'lit';
    import './greeting.js';
    
    import Koa from 'koa';
    import koaNodeResolve from 'koa-node-resolve';
    import { Readable } from 'stream';
     
    const { nodeResolve } = koaNodeResolve;
    const app = new Koa();
    const port = 8080;
    
    app.use(async (ctx) => {
      ctx.type = 'text/html';
      ctx.body = Readable.from(render(html`
        <simple-greeting></simple-greeting>
        <simple-greeting name="SSR Works!"></simple-greeting>
      `));
    });
    app.use(nodeResolve({}));
    
    app.listen(port, () => {
      console.log(`Server listening on port ${port}`);
    });

Live Reproduction Link

https://stackblitz.com/github/thescientist13/lit-ssr-create-render-root

Expected Results

No Declarative Shadow DOM would be present, just like in the Lit Playground

<simple-greeting>
    <style>p { color: blue }</style>
    <!--lit-part EvGichL14uw=--><p>Hello, <!--lit-part-->Somebody<!--/lit-part-->!</p><!--/lit-part-->
</simple-greeting>

Screen Shot 2022-06-23 at 9 27 51 PM

Actual Results

Declarative Shadow DOM is still rendered

<simple-greeting>
  <template shadowroot="open">
    <style>p { color: blue }</style>
    <!--lit-part EvGichL14uw=--><p>Hello, <!--lit-part-->Somebody<!--/lit-part-->!</p><!--/lit-part-->
  </template>
</simple-greeting>

Screen Shot 2022-06-23 at 9 42 18 PM

Browsers Affected

  • Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11
  • NodeJS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    📋 Triaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions