Skip to content

Commit 0007acb

Browse files
committed
fix(hydrate): fix hydrating style elements
1 parent aa97f95 commit 0007acb

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/runtime/client-hydrate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ const clientHydrate = (
214214
}
215215
}
216216
}
217+
} else if (parentVNode && parentVNode.$tag$ === 'style') {
218+
parentVNode.$children$ = [{
219+
$index$: '0',
220+
$text$: node.textContent,
221+
$elm$: node
222+
} as any];
217223
}
218224
};
219225

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Component, h } from '@stencil/core';
2+
import { newSpecPage } from '@stencil/core/testing';
3+
4+
5+
describe('hydrate style element', () => {
6+
7+
it('style element text', async () => {
8+
@Component({ tag: 'cmp-a' })
9+
class CmpA {
10+
11+
render() {
12+
return (
13+
(h as any)('style', null, 'div { color: red; }')
14+
);
15+
}
16+
}
17+
const serverHydrated = await newSpecPage({
18+
components: [CmpA],
19+
html: `<cmp-a></cmp-a>`,
20+
hydrateServerSide: true
21+
});
22+
expect(serverHydrated.root).toEqualHtml(`
23+
<cmp-a s-id="1">
24+
<!--r.1-->
25+
<style c-id="1.0.0.0">div { color: red; }</style>
26+
</cmp-a>
27+
`);
28+
29+
const clientHydrated = await newSpecPage({
30+
components: [CmpA],
31+
html: serverHydrated.root.outerHTML,
32+
hydrateClientSide: true
33+
});
34+
35+
expect(clientHydrated.root).toEqualHtml(`
36+
<cmp-a>
37+
<!--r.1-->
38+
<style>div { color: red; }</style>
39+
</cmp-a>
40+
`);
41+
});
42+
43+
});

src/runtime/vdom/vdom-annotations.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ const insertChildVNodeAnnotations = (doc: Document, vnodeChild: d.VNode, cmpData
113113
childElm.setAttribute(HYDRATE_CHILD_ID, childId);
114114

115115
} else if (childElm.nodeType === NODE_TYPE.TextNode) {
116-
const textNodeId = `${TEXT_NODE_ID}.${childId}`;
116+
const parentNode = childElm.parentNode;
117+
if (parentNode.nodeName !== 'STYLE') {
118+
const textNodeId = `${TEXT_NODE_ID}.${childId}`;
117119

118-
const commentBeforeTextNode = doc.createComment(textNodeId);
119-
childElm.parentNode.insertBefore(commentBeforeTextNode, childElm);
120+
const commentBeforeTextNode = doc.createComment(textNodeId);
121+
parentNode.insertBefore(commentBeforeTextNode, childElm);
122+
}
120123

121124
} else if (childElm.nodeType === NODE_TYPE.CommentNode) {
122125
if (childElm['s-sr']) {

0 commit comments

Comments
 (0)