Skip to content
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

fix(hmr/keep-alive): fix error in reload component (#7042) #7049

Merged
merged 5 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/runtime-core/__tests__/hmr.spec.ts
Expand Up @@ -151,6 +151,73 @@ describe('hot module replacement', () => {
expect(mountSpy).toHaveBeenCalledTimes(1)
})

// #7042
test('reload KeepAlive slot', async () => {
const root = nodeOps.createElement('div')
const childId = 'test-child-keep-alive'
const unmountSpy = jest.fn()
const mountSpy = jest.fn()
const activeSpy = jest.fn()
const deactiveSpy = jest.fn()

const Child: ComponentOptions = {
__hmrId: childId,
data() {
return { count: 0 }
},
unmounted: unmountSpy,
render: compileToFunction(`<div>{{ count }}</div>`)
}
createRecord(childId, Child)

const Parent: ComponentOptions = {
components: { Child },
data() {
return { toggle: true }
},
render: compileToFunction(
`<button @click="toggle = !toggle"></button><KeepAlive><Child v-if="toggle" /></KeepAlive>`
)
}

render(h(Parent), root)
expect(serializeInner(root)).toBe(`<button></button><div>0</div>`)

reload(childId, {
__hmrId: childId,
data() {
return { count: 1 }
},
mounted: mountSpy,
unmounted: unmountSpy,
activated: activeSpy,
deactivated: deactiveSpy,
render: compileToFunction(`<div>{{ count }}</div>`)
})
await nextTick()
expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(1)
expect(deactiveSpy).toHaveBeenCalledTimes(0)

// should not unmount when toggling
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(1)
expect(deactiveSpy).toHaveBeenCalledTimes(1)

// should not mount when toggling
triggerEvent(root.children[1] as TestElement, 'click')
await nextTick()
expect(unmountSpy).toHaveBeenCalledTimes(1)
expect(mountSpy).toHaveBeenCalledTimes(1)
expect(activeSpy).toHaveBeenCalledTimes(2)
expect(deactiveSpy).toHaveBeenCalledTimes(1)
})

test('reload class component', async () => {
const root = nodeOps.createElement('div')
const childId = 'test4-child'
Expand Down
4 changes: 1 addition & 3 deletions packages/runtime-core/src/components/KeepAlive.ts
Expand Up @@ -31,7 +31,6 @@ import {
invokeArrayFns
} from '@vue/shared'
import { watch } from '../apiWatch'
import { hmrDirtyComponents } from '../hmr'
import {
RendererInternals,
queuePostRenderEffect,
Expand Down Expand Up @@ -281,8 +280,7 @@ const KeepAliveImpl: ComponentOptions = {

if (
(include && (!name || !matches(include, name))) ||
(exclude && name && matches(exclude, name)) ||
(__DEV__ && hmrDirtyComponents.has(comp))
(exclude && name && matches(exclude, name))
) {
current = vnode
return rawVNode
Expand Down
8 changes: 8 additions & 0 deletions packages/runtime-core/src/vnode.ts
Expand Up @@ -352,6 +352,14 @@ export function isSameVNodeType(n1: VNode, n2: VNode): boolean {
n2.shapeFlag & ShapeFlags.COMPONENT &&
hmrDirtyComponents.has(n2.type as ConcreteComponent)
) {
// #7042, ensure the vnode being unmounted during HMR
if (n1.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
n1.shapeFlag -= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
}
// #7042, ensure the vnode being mounted as fresh during HMR
if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
n2.shapeFlag -= ShapeFlags.COMPONENT_KEPT_ALIVE
}
// HMR only: if the component has been hot-updated, force a reload.
return false
}
Expand Down