Skip to content

fix(ssr): ignore empty text vnode when hydrating (fix #11109) #11111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/vdom/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isDef,
isUndef,
isTrue,
isFalse,
makeMap,
isRegExp,
isPrimitive
Expand Down Expand Up @@ -643,7 +644,13 @@ export function createPatchFunction (backend) {
let childrenMatch = true
let childNode = elm.firstChild
for (let i = 0; i < children.length; i++) {
if (!childNode || !hydrate(childNode, children[i], insertedVnodeQueue, inVPre)) {
const child = children[i]
// ignore empty text vnode but create a empty textNode for next patch
if (isUndef(child.tag) && isFalse(child.isComment) && child.text === '') {
createElm(child, insertedVnodeQueue, elm, childNode)
continue
Copy link

@contribu contribu Feb 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in child.elm === undefined and causes a crash in next patch.
Reproduction: deqwin/vue@fix_ssr_hydration...contribu:fix_ssr_hydration_crash

I think it is better to treat empty string nodes like #5117

Copy link
Author

@deqwin deqwin Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact is not that the vnode tree has an extra empty text node, but the DOM tree is missing a necessary empty text node.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva I have modified the code to deal with this problem and also updated the test.

Copy link

@contribu contribu Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I am unsure if this causes reflow during hydration and if the existing hydration code allows it.
I think it shouldn't cause reflow if the existing hydration code disallows reflow.

Copy link
Author

@deqwin deqwin Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it‘s worth further studying. However, it is necessary to keep the vnode structure and DOM structure consistent at all times. So I think it's ok to do this step here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution is rendering empty text nodes as comment nodes in SSR like async placeholder or v-if="false".
I am unsure which solution is better.

}
if (!childNode || !hydrate(childNode, child, insertedVnodeQueue, inVPre)) {
childrenMatch = false
break
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/modules/vdom/patch/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,24 @@ describe('vdom patch: hydration', () => {
expect(dom.children[0].className).toBe('bar')
}).then(done)
})

// #11109
it('should not fail hydration with empty text vnodes children', done => {
const dom = createMockSSRDOM('<div class="bar"><span>bar</span></div>')

const vm = new Vue({
data: {
a: ''
},
template: `<div><div class="bar"><span>bar</span>{{a}}</div></div>`
}).$mount(dom)

expect('not matching server-rendered content').not.toHaveBeenWarned()

// should update
vm.a = 'foo'
waitForUpdate(() => {
expect(dom.children[0].innerHTML).toBe('<span>bar</span>foo')
}).then(done)
})
})