Skip to content

fix(patch): static tree as same vnode(#8021) #8499

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 1 commit 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
15 changes: 10 additions & 5 deletions src/core/vdom/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function sameVnode (a, b) {
isTrue(a.isAsyncPlaceholder) &&
a.asyncFactory === b.asyncFactory &&
isUndef(b.asyncFactory.error)
)
) || sameStaticVnode(a, b)
)
)
}
Expand All @@ -57,6 +57,12 @@ function sameInputType (a, b) {
return typeA === typeB || isTextInputType(typeA) && isTextInputType(typeB)
}

function sameStaticVnode (a, b) {
return isTrue(a.isStatic) &&
isTrue(b.isStatic) &&
(isTrue(b.isCloned) || isTrue(b.isOnce))
}

function createKeyToOldIdx (children, beginIdx, endIdx) {
let i, key
const map = {}
Expand Down Expand Up @@ -518,10 +524,9 @@ export function createPatchFunction (backend) {
// note we only do this if the vnode is cloned -
// if the new node is not cloned it means the render functions have been
// reset by the hot-reload-api and we need to do a proper re-render.
if (isTrue(vnode.isStatic) &&
isTrue(oldVnode.isStatic) &&
vnode.key === oldVnode.key &&
(isTrue(vnode.isCloned) || isTrue(vnode.isOnce))
if (
sameStaticVnode(oldVnode, vnode) &&
vnode.key === oldVnode.key
) {
vnode.componentInstance = oldVnode.componentInstance
return
Expand Down
36 changes: 36 additions & 0 deletions test/unit/features/directives/once.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,42 @@ describe('Directive v-once', () => {
}).then(done)
})

it('should work inside v-for in component', done => {
const vm = new Vue({
data: {
comp: 'comp1',
list: [
{ id: 0 }
]
},
components: {
comp1: {
template: '<span>comp1</span>'
},
comp2: {
template: '<span>comp2</span>'
}
},
template: `
<div>
<div v-for="i in list" :key="i.id">
<component :is='comp' v-once></component>
</div>
</div>
`
}).$mount()

expect(vm.$el.textContent).toBe('comp1')

vm.comp = 'comp2'
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('comp1')
vm.list.push({ id: 1 })
}).then(() => {
expect(vm.$el.textContent).toBe('comp1comp2')
}).then(done)
})

it('should work inside v-for with v-if', done => {
const vm = new Vue({
data: {
Expand Down