Repository navigation
Description
Stencil Version
5.0.0-rc.12
Current Behavior
The parse gate for streamed server markup (#7005) releases a host too early when a script inserts a node into the document while the parser is still inside that host.
The gate treats a host as parsed once the host or one of its ancestors has a next sibling. A node that a script appends to an ancestor, e.g. document.body.appendChild(container) for a portal, a toast container or a script tag, is such a sibling, although the parser has not finished the host.
Depending on where the parser is at that moment:
- Before the
<template shadowrootmode>of the host: Stencil attaches its own shadow root, and the parser can no longer attach the declarative one. Chromium logsA second declarative shadow root cannot be created on a host, the<template>stays in the light DOM of the host, and the component renders on the client instead of hydrating. This is the outcome Streaming SSR: declarative shadow DOM conflicts with attachShadow in the constructor #6690 describes. - Inside the
<template>: Stencil hydrates the part of the shadow root that has arrived and renders the rest. The parser then appends the rest of the server markup to the same nodes, so the content is there twice.
The parser stops inside a host whenever a chunk of the response ends there, so this needs no unusual markup, only a script that runs in that pause.
Expected Behavior
A host is held until the parser has finished it, whatever scripts do to the document meanwhile. In the reproduction all three pages should end like control:
light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot></div>
System Info
System: node 22.21.0
Platform: darwin (27.0.0)
CPU Model: Apple M2 Pro (12 cpus)
Stencil: 5.0.0-rc.12 🔈
TypeScript: 6.0.3
Rolldown: 1.3.3
Terser: 5.51.4
Browsers: Chromium and WebKit (Playwright 1.63)Steps to Reproduce
- Clone https://github.com/henrijoss/stencil-v5-repros and run
npm install npm install @stencil/[email protected](the repository is pinned to rc.10, which behaves the same)npm start --workspace=ssr-streamingand open http://localhost:3336- Open
control,insert-before-templateandinsert-inside-template
The project has a loader-bundle and an ssr output target. The server renders <repro-ssr label="Server label"><span>Slotted</span></repro-ssr> with renderToString(), starts the response, waits until the component is defined, sends the markup up to the middle of the host, and sends the rest once the script of the page has run. Each page prints the light DOM children and the shadow root of the host:
control, nothing is inserted:
light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot></div>
insert-before-template, the page appends a <div> to <body> after the opening tag of the host has arrived:
light DOM children of the host: template, span
shadow root: <div><p>Server label</p><slot></slot></div>
plus A second declarative shadow root cannot be created on a host. in the console of Chromium.
insert-inside-template, the same after <template shadowrootmode="open"><div c-id="…"> has arrived:
light DOM children of the host: span
shadow root: <div><p>Server label</p><slot></slot><p c-id="3.1.1.0"><!--t.3.2.2.0-->Server label</p><slot c-id="3.3.1.1"></slot></div>
Code Reproduction URL
Additional Information
The check is isParsed() in packages/core/src/runtime/parse-gate.ts:
const isParsed = (elm) => {
if (win.document.readyState !== 'loading') return true;
for (let n = elm; n; n = parentOf(n)) if (n.nextSibling) return true;
return false;
};A next sibling only proves that the parser has moved on when the parser inserted it. flush() runs for every mutation of the document, so the first mutation after such an insertion releases every host that is still open below that ancestor.
I have not tried a fix. A signal that scripts of the page can't produce by accident would close it, e.g. a marker the server writes as the last node of a host, or of its template.
This is not a regression from Stencil 4, which has no parse gate and always fails for markup that is streamed after the components are defined (#6690). It is a gap in the fix for that.
I found no existing issue for this.
Activity
fix(components,components-react): hold a streamed component until its…
henrijoss commentedon Nov 26, 2026
There is a third position of the parser where this matters, which I missed above: after the
<template>of the host and before its light DOM children.The host is then released with a complete shadow root, so nothing looks broken at first, but the component hydrates before its children exist. A component that reads its children when it loads or renders, e.g. to check whether a slot is filled, gets a different result than on the server and doesn't find out when the children arrive.
I added it to the reproduction (https://github.com/henrijoss/stencil-v5-repros/tree/main/ssr-streaming, same steps,
5.0.0-rc.12).<repro-ssr-children>rendersthis.host.children.length, and the server renders<repro-ssr-children><span>Slotted</span></repro-ssr-children>aschildren: 1.control-children, nothing is inserted:insert-before-children, the page appends a<div>to<body>after</template>of the host has arrived and before<span>Slotted</span>has:Same in Chromium and WebKit.
This also corrects my suggestion: a marker at the end of the template would not be enough. Whatever tells that the host is complete has to come after its children, as the last node of the host.
johnjenkins commentedon Nov 27, 2026
TYSM for trying out v5. This was fixed in 5.0.0-rc.13 🥳
fix(components,components-react): hold a streamed component until the…