bug: hydration removes the host classes of a child component that renders before its parent
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.45.1 (also 4.43.5;)
Current Behavior
After server-side rendering (renderToString from dist-hydrate-script, serializeShadowRoot: 'declarative-shadow-dom') and client-side hydration with dist-custom-elements, a child
component that hydrates and renders before its parent loses every class it set on its own host,
including the hydrated flag. Nothing adds them again, so the child keeps a wrong host state
until one of its own classes changes.
@Component({ tag: 'my-child', shadow: true })
export class MyChild {
@Prop() disabled = false;
render() {
return (
<Host class={{ 'child-own': true, 'child-disabled': this.disabled }}>
<slot />
</Host>
);
}
}
@Component({ tag: 'my-parent', shadow: true })
export class MyParent {
render() {
return (
<Host>
<my-child id="with-class" class={{ 'parent-set': true }} disabled>A</my-child>
<my-child id="without-class" disabled>B</my-child>
</Host>
);
}
}After hydration, with my-child defined before my-parent:
| Element | Classes after CSR | Classes after SSR and hydration |
|---|---|---|
#with-class |
parent-set child-own child-disabled hydrated |
parent-set child-disabled |
#without-class |
child-own child-disabled hydrated |
child-disabled |
child-own and the hydrated flag are lost in both children (measured in Chrome 154).
This is not an edge case in practice: defineCustomElement() of a dist-custom-elements build
defines the component's dependencies first, so nested components are always upgraded and rendered
before their parent. In Siemens iX 5.2.1, for example, the ix-dropdown-item in every
ix-select-item ends up without hydrated, ix-focusable and disabled after SSR, so disabled
options look enabled.
Expected Behavior
SSR plus hydration ends up with the same host classes as client-side rendering: the classes the child set on its own host and the hydrated flag stay, the classes of the parent's vnode are applied, and classes that the parent rendered on the server but not on the client are removed.
System Info
System: node 24.15.0
Platform: darwin (27.0.0)
Stencil: 4.45.1 (main, e2e5dfc05)
TypeScript: 5.8.3
Rollup: 4.44.0
Browsers: Chrome 154 (wdio), Chromium 153 and WebKit 26.6 (Playwright 1.63)Steps to Reproduce
Clone https://github.com/marcomattes/stencil-hydrate-host-classes (
@stencil/core4.45.1, the two components above,dist-custom-elementsanddist-hydrate-script).npm install,npm run build.npm start, then openhttp://localhost:3000/csr(client-side rendering only) andhttp://localhost:3000/ssr(renderToStringwithserializeShadowRoot: 'declarative-shadow-dom', then hydrated). Both pages definemy-childbeforemy-parent, asdefineCustomElement()does for dependencies, and print the host classes of bothmy-childelements.Or run
npm run check, which compares both pages in Playwright's Chromium and exits with 1 when they differ:with-class CSR: child-disabled child-own hydrated parent-set SSR: child-disabled parent-set <-- differs without-class CSR: child-disabled child-own hydrated SSR: child-disabled <-- differs
Code Reproduction URL
https://github.com/marcomattes/stencil-hydrate-host-classes
Additional Information
Cause. clientHydrate() (src/runtime/client-hydrate.ts) seeds the old vnode of every child
element with the element's full server className:
// If we don't add the initial classes to the VNode, the first `vdom-render.ts` patch
// won't try to reconcile them. Classes set on the node will be blown away.
$attrs$: { class: node.className || '' },For a child component, that string also contains the classes it set on its own host and the
hydrated flag. On the parent's first render, setAccessor() (src/runtime/vdom/set-accessor.ts)
removes every old class that is not in the new vnode, both when the parent sets a class and when
it sets none (updateElement() then calls setAccessor() with undefined):
classList.remove(...oldClasses.filter((c) => c && !newClasses.includes(c)));When the child has already rendered, it does not render again, so its classes stay removed. When
the parent renders first, the child's own first render adds them again, which is why the lazy
dist build (where a child waits for its ancestor) is not affected. For scoped: true components,
the s-si / s-sc branch of setAccessor() already only adds classes on the initial render.
The seeding came in with #6082 (4.23.1), which made the first patch reconcile server classes.
Suggested fix. On the first render after hydration, skip the removal of the classes that an
already rendered child component set on its own host (the classes of its <Host> vnode and the
hydrated flag). Classes that the parent rendered on the server but not on the client are still
removed. I have a branch with this change, 5 unit tests for setAccessor() and a wdio test
(test/wdio/ssr-hydration/class-cmp.test.tsx, custom elements, child defined first) and can open
a PR.
Related. siemens/ix#2668 (the application sets className and removes the hydrated class,
no SSR) shows a similar symptom with another trigger.
Source: stenciljs/core