Skip to content

Commit dc229bc

Browse files
committed
wip(hydration): hydrate multi-element static nodes
1 parent 1184118 commit dc229bc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/runtime-core/__tests__/hydration.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,31 @@ describe('SSR hydration', () => {
5656
test('static', () => {
5757
const html = '<div><span>hello</span></div>'
5858
const { vnode, container } = mountWithHydration(html, () =>
59-
createStaticVNode(html)
59+
createStaticVNode('', 1)
6060
)
6161
expect(vnode.el).toBe(container.firstChild)
6262
expect(vnode.el.outerHTML).toBe(html)
63+
expect(vnode.anchor).toBe(container.firstChild)
64+
expect(vnode.children).toBe(html)
65+
})
66+
67+
test('static (multiple elements)', () => {
68+
const staticContent = '<div></div><span>hello</span>'
69+
const html = `<div><div>hi</div>` + staticContent + `<div>ho</div></div>`
70+
71+
const n1 = h('div', 'hi')
72+
const s = createStaticVNode('', 2)
73+
const n2 = h('div', 'ho')
74+
75+
const { container } = mountWithHydration(html, () => h('div', [n1, s, n2]))
76+
77+
const div = container.firstChild!
78+
79+
expect(n1.el).toBe(div.firstChild)
80+
expect(n2.el).toBe(div.lastChild)
81+
expect(s.el).toBe(div.childNodes[1])
82+
expect(s.anchor).toBe(div.childNodes[2])
83+
expect(s.children).toBe(staticContent)
6384
})
6485

6586
test('element with text children', async () => {

packages/runtime-core/src/hydration.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,18 @@ export function createHydrationFunctions(
117117
if (domType !== DOMNodeTypes.ELEMENT) {
118118
return onMismatch()
119119
}
120-
return nextSibling(node)
120+
// determine anchor, adopt content
121+
let content = ''
122+
let cur = node
123+
for (let i = 0; i < vnode.staticCount; i++) {
124+
content += (cur as Element).outerHTML
125+
if (i === vnode.staticCount - 1) {
126+
vnode.anchor = cur
127+
}
128+
cur = nextSibling(cur)!
129+
}
130+
vnode.children = content
131+
return cur
121132
case Fragment:
122133
if (!isFragmentStart) {
123134
return onMismatch()

0 commit comments

Comments
 (0)