Skip to content

Commit

Permalink
relax SSR hydration match check to allow client populating empty pare…
Browse files Browse the repository at this point in the history
…nt nodes
  • Loading branch information
yyx990803 committed Sep 8, 2016
1 parent 4afccc8 commit 7a2c986
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
55 changes: 32 additions & 23 deletions src/core/vdom/patch.js
Expand Up @@ -117,13 +117,7 @@ export function createPatchFunction (backend) {
? nodeOps.createElementNS(vnode.ns, tag)
: nodeOps.createElement(tag)
setScope(vnode)
if (Array.isArray(children)) {
for (i = 0; i < children.length; ++i) {
nodeOps.appendChild(elm, createElm(children[i], insertedVnodeQueue, true))
}
} else if (isPrimitive(vnode.text)) {
nodeOps.appendChild(elm, nodeOps.createTextNode(vnode.text))
}
createChildren(vnode, children, insertedVnodeQueue)
if (isDef(data)) {
invokeCreateHooks(vnode, insertedVnodeQueue)
}
Expand All @@ -135,6 +129,16 @@ export function createPatchFunction (backend) {
return vnode.elm
}

function createChildren (vnode, children, insertedVnodeQueue) {
if (Array.isArray(children)) {
for (let i = 0; i < children.length; ++i) {
nodeOps.appendChild(vnode.elm, createElm(children[i], insertedVnodeQueue, true))
}
} else if (isPrimitive(vnode.text)) {
nodeOps.appendChild(vnode.elm, nodeOps.createTextNode(vnode.text))
}
}

function isPatchable (vnode) {
while (vnode.child) {
vnode = vnode.child._vnode
Expand Down Expand Up @@ -404,26 +408,31 @@ export function createPatchFunction (backend) {
if (isDef(tag)) {
if (isDef(children)) {
const childNodes = nodeOps.childNodes(elm)
let childrenMatch = true
if (childNodes.length !== children.length) {
childrenMatch = false
// empty element, allow client to pick up and populate children
if (!childNodes.length) {
createChildren(vnode, children, insertedVnodeQueue)
} else {
for (let i = 0; i < children.length; i++) {
if (!hydrate(childNodes[i], children[i], insertedVnodeQueue)) {
childrenMatch = false
break
let childrenMatch = true
if (childNodes.length !== children.length) {
childrenMatch = false
} else {
for (let i = 0; i < children.length; i++) {
if (!hydrate(childNodes[i], children[i], insertedVnodeQueue)) {
childrenMatch = false
break
}
}
}
}
if (!childrenMatch) {
if (process.env.NODE_ENV !== 'production' &&
typeof console !== 'undefined' &&
!bailed) {
bailed = true
console.warn('Parent: ', elm)
console.warn('Mismatching childNodes vs. VNodes: ', childNodes, children)
if (!childrenMatch) {
if (process.env.NODE_ENV !== 'production' &&
typeof console !== 'undefined' &&
!bailed) {
bailed = true
console.warn('Parent: ', elm)
console.warn('Mismatching childNodes vs. VNodes: ', childNodes, children)
}
return false
}
return false
}
}
if (isDef(data)) {
Expand Down
31 changes: 29 additions & 2 deletions test/unit/modules/vdom/patch/hydration.spec.js
Expand Up @@ -141,9 +141,36 @@ describe('vdom patch: hydration', () => {
template: '<div><span>{{a}}</span></div>'
}
}
})
}).$mount(dom)

vm.$mount(dom)
expect('not matching server-rendered content').toHaveBeenWarned()
})

it('should pick up elements with no children and populate without warning', done => {
const dom = document.createElement('div')
dom.setAttribute('server-rendered', 'true')
dom.innerHTML = '<div><span></span></div>'
const span = dom.querySelector('span')

const vm = new Vue({
template: '<div><test></test></div>',
components: {
test: {
data () {
return { a: 'qux' }
},
template: '<div><span>{{a}}</span></div>'
}
}
}).$mount(dom)

expect('not matching server-rendered content').not.toHaveBeenWarned()
expect(span).toBe(vm.$el.querySelector('span'))
expect(vm.$el.innerHTML).toBe('<div><span>qux</span></div>')

vm.$children[0].a = 'foo'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<div><span>foo</span></div>')
}).then(done)
})
})

1 comment on commit 7a2c986

@tiye
Copy link

@tiye tiye commented on 7a2c986 Sep 13, 2016

Choose a reason for hiding this comment

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

So now Vue would insert app html into <div id="vue-app" server-rendered="true"></div>? Looking forward to the docs.

Please sign in to comment.