Skip to content

Commit 3363017

Browse files
johnjenkinsJohn Jenkins
and
John Jenkins
authored
fix(ssr): add scoped classes during SSR (#6238)
* fix(ssr): add scoped classes to `scoped` ssr components * chore: add test * chore: lint * chore: only on server --------- Co-authored-by: John Jenkins <john.jenkins@nanoporetech.com>
1 parent 2f712bc commit 3363017

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Diff for: src/runtime/vdom/vdom-render.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ const createElm = (oldParentVNode: d.VNode, newParentVNode: d.VNode, childIndex:
121121
updateElement(null, newVNode, isSvgMode);
122122
}
123123

124-
if (BUILD.scoped && isDef(scopeId) && elm['s-si'] !== scopeId) {
124+
if (
125+
(BUILD.scoped || (BUILD.hydrateServerSide && CMP_FLAGS.shadowNeedsScopedCss)) &&
126+
isDef(scopeId) &&
127+
elm['s-si'] !== scopeId
128+
) {
125129
// if this element is `scoped: true` all internal
126130
// children required the scope id class for styling
127131
elm.classList.add((elm['s-si'] = scopeId));
@@ -183,7 +187,7 @@ const createElm = (oldParentVNode: d.VNode, newParentVNode: d.VNode, childIndex:
183187
putBackInOriginalLocation(oldParentVNode.$elm$, false);
184188
}
185189
}
186-
if (BUILD.scoped) {
190+
if (BUILD.scoped || (BUILD.hydrateServerSide && CMP_FLAGS.shadowNeedsScopedCss)) {
187191
addRemoveSlotScopedClass(contentRef, elm, newParentVNode.$elm$, oldParentVNode?.$elm$);
188192
}
189193
}

Diff for: test/wdio/ssr-hydration/cmp.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ describe('Sanity check SSR > Client hydration', () => {
151151
it('resolves slots correctly during client-side hydration', async () => {
152152
await testSuite.slots();
153153
});
154+
155+
it('checks renderToString adds scoped class names', async () => {
156+
const { html } = await renderToString(
157+
`
158+
<ssr-shadow-cmp>
159+
<p>Default slot content</p>
160+
<p slot="client-only">Client-only slot content</p>
161+
</ssr-shadow-cmp>
162+
`,
163+
{
164+
fullDocument: true,
165+
serializeShadowRoot: 'scoped',
166+
constrainTimeouts: false,
167+
prettyHTML: false,
168+
},
169+
);
170+
// standard scoped class + ::slotted scoped class
171+
expect(html).toContain('sc-ssr-shadow-cmp sc-ssr-shadow-cmp-s');
172+
});
154173
});
155174

156175
describe('custom-elements / declarative-shadow-dom', () => {

0 commit comments

Comments
 (0)