Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
This is a suggestion I'd like to discuss, related to SSR streaming of web components in general.
When a server-rendered web component is streamed to the browser the HTML parser fires the custom element constructor synchronously when it sees the host's start tag, before parsing the element's children (including the <template shadowrootmode>). Stencil, however, calls attachShadow in the component's constructor, with a guard that checks whether a shadow root already exists. In most cases that guard is sufficient, but during streaming the constructor runs the moment the host's start tag is parsed, before the parser has reached the declarative shadow root template, and before the element is connected to the DOM. At that point the element effectively has no shadow root and no attributes yet, so any inspection of the DOM node returns "empty" results and the guard always passes.
Minimal reproduction example
The console shows: A second declarative shadow root cannot be created on a host. The same thing happens during streaming SSR: the component's JS is already registered and running by the time the parser encounters the <template shadowrootmode>, so the parser's later attempt to attach the declarative shadow root fails because the constructor has already attached one.
Has this come up before, or is it a known issue on your side?
Describe the Use Case
Streaming SSR rendered web components
Describe Preferred Solution
Maybe one approach would be to defer the attachShadow call to connectedCallback, where the element is actually attached to the DOM and its attributes are readable. The HTML parser still isn't guaranteed to have finished parsing the element's children at that point, but we can reliably read attributes on the host, which is enough to decide whether we're hydrating a DSR-rendered component or falling back to client-side rendering. Still we would need a way to reliably tell if the HTML parser is finished before running hydration or any client side code. We would need to delay the component instantiation until the streaming finished and the <template> was upgraded.
Describe Alternatives
No response
Related Code
I'm not entirely sure how this is meant to be handled by the SSR solutions Stencil provides, but in my testing streamed components consistently bail out to client-side rendering, which is presumably why the error doesn't surface in practice. Unfortunately it cannot easily be reproduced in Codesandbox/Stackblitz, because JavaScript has to be disabled to actually observe the bailout. To force the component to be streamed, I used the following setup in a Next.js (App Router) page:
Reproduction example
Additional Information
Relevant guidance from the HTML spec:
"In general, work should be deferred to connectedCallback as much as possible—especially work involving fetching resources or rendering. However, note that connectedCallback can be called more than once, so any initialization work that is truly one-time will need a guard to prevent it from running twice.
In general, the constructor should be used to set up initial state and default values, and to set up event listeners and possibly a shadow root."
Activity
stephanschroeter commentedon Jun 18, 2026
@johnjenkins, do you already know when this issue will be tackled? :-)
johnjenkins commentedon Jun 18, 2026
no idea @stephanschroeter sorry.
I'm mainly concentrating on v5 development atm.
I did have a brief look to see if it was an easy win, but found it was very complex with Stencil's current hydration ordering and resolution
visualkhh commentedon Nov 1, 2026
I ran the exact race in Chromium, and the native resolution points at the fix: silently drop, don't throw.
Setup: custom element whose constructor calls
attachShadow(), markup<x-dsd><template shadowrootmode="open"><span>in-dsd</span></template></x-dsd>(constructor runs at the host start tag, before the parser reaches the template — the streaming order):attachShadow()succeeds (no root yet)shadowRoot.innerHTML→""— the declarative template is dropped silentlySo the HTML parser's own rule for this race is first-attacher-wins with silent drop, and Stencil's
"A second declarative shadow root cannot be created on a host"throw diverges from it. The streaming case in the report hits the throw path precisely because the guard (shadow root already exists?) is evaluated too early to see the template that hasn't been parsed yet.Suggested fix direction: make the DSD attachment tolerant — if the host already has a shadow root when the
<template shadowrootmode>is reached, skip the template (native behavior) instead of throwing. That unifies streaming and buffered parsing under one rule and removes the ordering sensitivity entirely, since constructor-attached and parser-attached roots converge to the same outcome: exactly one root, first one wins.Same race verified on what I use (@dooboostore/dom-parser, which powers @dooboostore/simple-web-component SSR): constructor attach wins, template content is dropped without error, final root identical to Chromium's. Happy to share the probe if the fix needs a cross-engine acceptance case.