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 report this problem, without success.
Stencil Version
4.41.0 – 4.45.2 (4.40.1 works)
Current Behavior
A non-shadow component renders an element into the default <slot> of another non-shadow component. After SSR (hydrate app, renderToString) and client hydration, the runtime sets hidden="" on that element, so the content disappears from the page.
<!-- SSR output (correct) -->
<x-page class="hydrated" s-id="1"><!--r.1--><x-wrap class="hydrated" c-id="1.0.0.0" s-id="2"><!--r.2--><!--o.1.1--><!--s.2.0.0.0.--><div class="inner" c-id="1.1.1.0" s-sn=""><!--t.1.2.2.0-->Rendered by x-page into the default slot of x-wrap</div></x-wrap></x-page>
<!-- after client hydration (4.41.0 … 4.45.2) -->
<x-page class="hydrated"><!--r.1--><x-wrap class="hydrated"><!--r.2--><!--s.2.0.0.0.--><div class="inner" hidden="">Rendered by x-page into the default slot of x-wrap</div></x-wrap></x-page>In a real app this is very visible: every layout section that renders its content into a wrapper component's slot goes blank once the client hydrates (big layout shift, the LCP element disappears). Client-only rendering (no SSR) is not affected, which makes it easy to miss.
Expected Behavior
The element stays visible after hydration — as in 4.40.1, and as in client-only rendering.
System Info
System: node 24.21.0
Platform: darwin (25.3.0)
CPU Model: Apple M3 Pro (11 cpus)
Stencil: 4.45.2
TypeScript: 5.8.3
Rollup: 4.44.0
Parse5: 7.2.1
Terser: 5.37.0
Browser: Chrome 148.0.7778.97 (puppeteer 24)Steps to Reproduce
Six files (inline below), then:
npm install
npx stencil build
node check.mjs # SSR <x-page>, open it in headless Chrome, report whether .inner is hiddenResult by version (same files, only @stencil/core changed): 4.40.1 → OK: .inner is visible; 4.41.0 and 4.45.2 → BUG: .inner is hidden after hydration.
package.json
{
"name": "stencil-ssr-slot-hidden-repro",
"private": true,
"type": "module",
"scripts": {
"build": "stencil build",
"check": "node check.mjs"
},
"devDependencies": {
"@stencil/core": "4.45.2",
"puppeteer": "^24.0.0"
}
}tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"lib": ["dom", "es2017"],
"moduleResolution": "node",
"module": "esnext",
"target": "es2017",
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment"
},
"include": ["src"]
}stencil.config.ts
import { Config } from '@stencil/core';
export const config: Config = {
namespace: 'repro',
outputTargets: [{ type: 'www', serviceWorker: null }, { type: 'dist-hydrate-script' }],
};src/components/x-wrap/x-wrap.tsx
import { Component, Host, h } from '@stencil/core';
/** A light-DOM layout wrapper: projects its children through a default slot. */
@Component({ tag: 'x-wrap', shadow: false })
export class XWrap {
render() {
return (
<Host>
<slot />
</Host>
);
}
}src/components/x-page/x-page.tsx
import { Component, h } from '@stencil/core';
/** Renders its own content into x-wrap's default slot. */
@Component({ tag: 'x-page', shadow: false })
export class XPage {
render() {
return (
<x-wrap>
<div class="inner">Rendered by x-page into the default slot of x-wrap</div>
</x-wrap>
);
}
}check.mjs
// Server-render <x-page> with the hydrate build, load it in Chrome with the client build, and report
// whether the content x-page rendered into <x-wrap>'s slot ends up hidden after client hydration.
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import puppeteer from 'puppeteer';
import { renderToString } from './hydrate/index.js';
const doc = `<!doctype html><html><head><script type="module" src="/build/repro.esm.js"></script></head><body><x-page></x-page></body></html>`;
const { html } = await renderToString(doc, { fullDocument: true, prettyHtml: false });
fs.writeFileSync('www/ssr.html', html);
console.log('SSR:', html.match(/<x-page[\s\S]*<\/x-page>/)[0]);
const server = http.createServer((req, res) => {
const file = path.join('www', req.url.split('?')[0]);
if (!fs.existsSync(file) || fs.statSync(file).isDirectory()) return res.writeHead(404).end();
res.writeHead(200, { 'content-type': file.endsWith('.js') ? 'text/javascript' : 'text/html' });
fs.createReadStream(file).pipe(res);
});
await new Promise(r => server.listen(0, r));
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`http://localhost:${server.address().port}/ssr.html`, { waitUntil: 'networkidle0' });
const inner = await page.$eval('.inner', el => ({ hidden: el.hidden, visible: el.checkVisibility() }));
console.log('After hydration:', await page.$eval('x-page', el => el.outerHTML));
console.log(inner.hidden ? 'BUG: .inner is hidden after hydration' : 'OK: .inner is visible');
await browser.close();
server.close();
process.exitCode = inner.hidden ? 1 : 0;Code Reproduction URL
The reproduction is fully inline above (two components + a check script); happy to push it to a repository if that helps.
Additional Information
Bisect: 4.40.1 good → 4.41.0 bad (also bad on 4.42.0, 4.43.5, 4.45.2). Crossing builds shows it is the client runtime: SSR markup from 4.45.2 hydrates correctly with the 4.40.1 client runtime, while SSR markup from 4.4.1 breaks with the 4.45.2 client runtime.
What happens (traced with logging added to the 4.45.2 client runtime):
initializeClientHydrate x-wrap id=2
addSlottedNodes host=2 slot='' node=inner sn-attr="" sn-prop=undefined <- not collected
initializeClientHydrate x-page id=1
clientHydrate host=1 converts s-sn on inner <- too late
renderVdom X-WRAP hides inner s-sh=undefined s-hn="X-PAGE"
- Since fix(runtime): update non-shadow slotted content visibility via dynamic
<slot>#6514 (4.41.0, "update non-shadow slotted content visibility via dynamic<slot>"), the post-render pass at the end ofrenderVdomruns for every non-shadow component with slot relocation (previously only behindextras.experimentalScopedSlotChangesand only forscopedcomponents): every light-DOM child of the host withs-hn !== hostTagNameand nos-shgetshidden = true. - During hydration,
s-shis set byaddSlotRelocateNodefor the nodes thataddSlottedNodes(src/runtime/client-hydrate.ts) collects after the slot's<!--s.…-->marker. For the default slot, an element is collected only if it has aslotattribute or ans-snproperty. - The SSR output marks
.innerwiths-sn=""as an attribute. It is turned into the property only inclientHydrateof the host that owns the node (c-id="1.…", i.e.x-page). Herex-wraphydrates first, so itsaddSlottedNodesskips.inner;.innernever getss-sh, and step 1 hides it. Nothing relocates it later, because SSR already placed it.
Possible fix (verified on 4.45.2 against this repro and a larger app; also confirmed by setting s-sh on the element before the loader runs): in addSlottedNodes, also accept an element whose s-sn attribute equals the slot name, since SSR has already assigned it to this slot even if its owning host has not hydrated yet:
do {
+ const ssrSlotName =
+ slottedNode && slottedNode.nodeType === NODE_TYPE.ElementNode ? (slottedNode as Element).getAttribute('s-sn') : null;
if (
slottedNode &&
- (((slottedNode['getAttribute'] && slottedNode.getAttribute('slot')) || slottedNode['s-sn']) === slotName ||
+ ((ssrSlotName !== null && ssrSlotName === slotName) ||
+ ((slottedNode['getAttribute'] && slottedNode.getAttribute('slot')) || slottedNode['s-sn']) === slotName ||
(slotName === '' &&Alternatives: make the post-render pass skip nodes that SSR placed into the slot, or set s-sh when the owning host converts s-sn. Note that the same pass is useful elsewhere (it correctly hides stale server markup in nodes cloned on the client), so going back to the old flag-gated behaviour would bring that problem back.
Possibly related: #6926 (hydration order with the child defined before its parent, shadow: true + scoped serialisation).
Activity
fix(runtime): keep SSR slot content visible across hydration order (#…