Skip to content

Commit de32524

Browse files
edison1105lynxlangya
authored andcommitted
fix(ssr): fix hydration for node with empty text node (vuejs#7216)
1 parent 0781bc3 commit de32524

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,21 @@ describe('SSR hydration', () => {
11601160
expect((vnode as any).component?.subTree.children[0].el).toBe(text)
11611161
})
11621162

1163+
// #7215
1164+
test('empty text node', () => {
1165+
const Comp = {
1166+
render(this: any) {
1167+
return h('p', [''])
1168+
}
1169+
}
1170+
const { container } = mountWithHydration('<p></p>', () => h(Comp))
1171+
expect(container.childNodes.length).toBe(1)
1172+
const p = container.childNodes[0]
1173+
expect(p.childNodes.length).toBe(1)
1174+
const text = p.childNodes[0]
1175+
expect(text.nodeType).toBe(3)
1176+
})
1177+
11631178
test('app.unmount()', async () => {
11641179
const container = document.createElement('DIV')
11651180
container.innerHTML = '<button></button>'

packages/runtime-core/src/hydration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ export function createHydrationFunctions(
541541
optimized,
542542
)
543543
} else if (vnode.type === Text && !vnode.children) {
544-
continue
544+
// #7215 create a TextNode for empty text node
545+
// because server rendered HTML won't contain a text node
546+
insert((vnode.el = createText('')), container)
545547
} else {
546548
hasMismatch = true
547549
if (

0 commit comments

Comments
 (0)