Skip to content

bug: v5 streamed SSR host inside a declarative shadow root is not hydrated until the document changes #7058

In review

Description

@henrijoss

Stencil Version

5.0.0-rc.14

Current Behavior

A server-rendered host that is streamed inside a declarative shadow root is not hydrated when the parser has finished it. It stays as the server sent it, with its s-id and its <!--e.{id}--> end marker, and without event listeners, until a node is added somewhere in the light DOM of the document or the document has loaded.

The parse gate (#7005, #7052) looks at its held hosts again in a MutationObserver on document. An observer on the document does not get the nodes the parser adds within a shadow root, so the end marker of such a host goes unnoticed. What releases the host is the next unrelated mutation of the document, or DOMContentLoaded.

This needs a shadow root that no held Stencil host owns, e.g. the declarative shadow root of a page shell or of a component of another library that contains the markup of renderToString(). A host in the declarative shadow root of another Stencil host is not affected: it waits for that host anyway, and the end marker of that host is in the document.

While a response is streamed, nothing may follow for seconds, and the component stays inert for that long.

Expected Behavior

A host is hydrated once its end marker has been parsed, wherever in the document it is. In the reproduction, shadow-root should show what control shows as soon as the host has arrived:

document: loading, end marker: removed, hydrated: true

System Info

      System: node 22.21.0
    Platform: darwin (27.0.0)
   CPU Model: Apple M2 Pro (12 cpus)
     Stencil: 5.0.0-rc.14 🛩
  TypeScript: 6.0.3
    Rolldown: 1.3.3
      Terser: 5.51.4

Browsers: Chromium and WebKit (Playwright 1.63)

Steps to Reproduce

  1. Clone https://github.com/henrijoss/stencil-v5-repros and run npm install
  2. npm start --workspace=ssr-shadow-root and open http://localhost:3337
  3. Open control and shadow-root

The project has a loader-bundle and an ssr output target. The server renders <repro-ssr-counter>, a button that counts its clicks, with renderToString(). It starts the response, waits until the component is defined, sends the whole host, and then keeps the response open. The page shows the state of the host in an input, so that showing it changes no node of the document.

control, the host is a child of a <section>:

document: loading, end marker: removed, hydrated: true

The button counts.

shadow-root, the host is in <section><template shadowrootmode="open">:

document: loading, end marker: <!--e.2-->, hydrated: false

The button does nothing. append a node to the document, which runs document.body.appendChild(document.createElement('div')), hydrates the host, and so does finish the response.

Code Reproduction URL

https://github.com/henrijoss/stencil-v5-repros/tree/main/ssr-shadow-root

Additional Information

The observer is set up in deferUntilParsed() in packages/core/src/runtime/parse-gate.ts:

observer = new MutationObserver(flush);
observer.observe(win.document, { childList: true, subtree: true });

subtree does not reach into shadow roots. I have not tried a fix in Stencil. Observing the held host as well would cover it, since the end marker is a child of the host:

observer.observe(elm, { childList: true });

This is not a regression: before #7052 the gate had the same observer. It is a gap that is left in the fix for #7049.

I found no existing issue for this.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions