Skip to content

fix(runtime): don't break declarative shadow DOM when SSR HTML is streamed - #7005

Merged
johnjenkins merged 1 commit into
v5from
feat/v5-support-ssr-streaming
Nov 11, 2026
Merged

johnjenkins merged 1 commit into
v5from
feat/v5-support-ssr-streaming

Conversation

@johnjenkins

@johnjenkins johnjenkins commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What is the current behavior?

When SSR HTML is streamed (e.g. Next.js App Router / Suspense), components are often already defined when the browser parses their markup. The browser then runs the element's constructor at the host's start tag, before it has parsed the children. Stencil attaches a shadow root in that constructor, so when the parser reaches the host's <template shadowrootmode> it can't attach the declarative root:

  • Chromium logs A second declarative shadow root cannot be created on a host.
  • The server-rendered shadow content is left behind as an inert <template> in the host's light DOM
  • The component re-renders on the client instead of hydrating
  • Nested shadow/scoped components can hydrate with slotted content missing or duplicated

Hydration has the same problem in general: it runs from connectedCallback, before the host's children have been parsed.

GitHub Issue Number: #6690

What is the new behavior?

SSR hosts (s-id) wait until the parser has finished their subtree before attaching a shadow root or hydrating.

  • New runtime/parse-gate.ts. While the document is loading, an SSR host whose subtree isn't parsed yet is held back. "Parsed" means the element or an ancestor has a next sibling. A MutationObserver (only active while something is held) and DOMContentLoaded release held hosts:
    • each host gets its shadow root (or reuses its declarative one) as soon as its own subtree is parsed
    • it only hydrates once no ancestor host is still held, so hydration runs parent-first, exactly like a fully parsed page. The existing DSD/scoped nesting logic is unchanged.
  • Lazy (loader-bundle) and standalone runtimes. The constructor only skips attaching the shadow root when the parser may still be streaming into the element. Any other element (created after load, or upgraded once its definition arrives) attaches in the constructor as before.
  • client-hydrate. The original-location comment scan re-runs when a host isn't in the map yet. Previously it ran once, so hosts streamed in later were missed.

Closes #6690

Documentation

BREAKING_CHANGES.md: "Shadow Roots of Parser-Created Elements Attach on Connect".

Does this introduce a breaking change?

  • Yes
  • No

A shadow component that the HTML parser creates while the document is still loading no longer has a shadow root in its constructor. It gets one when it connects, or, for an SSR host, once its subtree is parsed. Elements created any other way are unaffected.

Migration: don't read this.el.shadowRoot in a component constructor; use connectedCallback or a later lifecycle method.

Testing

  • New test/ssr/test/streaming.e2e.ts, run against both the lazy and standalone builds. It serves pages from a real chunked HTTP server, loads the components with an async script, and splits the stream inside every host. It covers:
    • a single DSD host (no browser warning, no leftover <template>, hydrated and interactive)
    • nested shadow/scoped hosts in both declarative-shadow-dom and scoped modes, compared with the same page served in one go
    • all 6 fail on v5 without this change and pass with it
  • Existing suites pass: core unit tests, test/ssr e2e, all four test/runtime projects, plus typecheck / lint / knip / spellcheck / format.
  • Manual check in Next.js 16 (App Router, production build) with SSR output streamed through <Suspense>:
    • before: the browser warning, leftover templates, client re-render, broken nested slot content
    • after: none of those; components hydrate the server-rendered DOM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant