Skip to content

Commit d296fe5

Browse files
committed
fix: avoid other bugs
1 parent 0046b79 commit d296fe5

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

packages/runtime-core/__tests__/components/KeepAlive.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ describe('KeepAlive', () => {
886886
test('should invoke onActivated of child on initial mount', async () => {
887887
let parentCount = 0
888888
let childCount = 0
889+
const toggle = ref(true)
889890
const Child = defineComponent({
890891
name: 'Child',
891892
setup() {
@@ -907,7 +908,7 @@ describe('KeepAlive', () => {
907908

908909
const App = {
909910
render: () => {
910-
return h(KeepAlive, null, () => h(AsyncComp))
911+
return h(KeepAlive, null, () => (toggle.value ? h(AsyncComp) : null))
911912
}
912913
}
913914

@@ -916,6 +917,13 @@ describe('KeepAlive', () => {
916917
expect(serializeInner(root)).toBe('child')
917918
expect(parentCount).toBe(1)
918919
expect(childCount).toBe(1)
920+
921+
toggle.value = false
922+
await timeout()
923+
toggle.value = true
924+
await timeout()
925+
expect(parentCount).toBe(2)
926+
expect(childCount).toBe(2)
919927
})
920928

921929
// #4976

packages/runtime-core/src/components/KeepAlive.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,10 @@ function registerKeepAliveHook(
401401
// arrays.
402402
if (target) {
403403
let current = target.parent
404-
let child = target
405404
while (current && current.parent) {
406405
if (isKeepAlive(current.parent.vnode)) {
407-
injectToKeepAliveRoot(
408-
wrappedHook,
409-
type,
410-
target,
411-
// #7276
412-
isAsyncWrapper(current) ? child : current
413-
)
406+
injectToKeepAliveRoot(wrappedHook, type, target, current)
414407
}
415-
child = current
416408
current = current.parent
417409
}
418410
}

packages/runtime-core/src/renderer.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1407,16 +1407,23 @@ function baseCreateRenderer(
14071407
)
14081408
}
14091409

1410+
const parentIsAsyncWrapperAndShouldKeepAlive =
1411+
parent &&
1412+
isAsyncWrapper(parent.vnode) &&
1413+
parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
14101414
// activated hook for keep-alive roots.
14111415
// #1742 activated hook must be accessed after first render
14121416
// since the hook may be injected by a child keep-alive
14131417
if (
14141418
initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE ||
1415-
(parent &&
1416-
isAsyncWrapper(parent.vnode) &&
1417-
parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE)
1419+
parentIsAsyncWrapperAndShouldKeepAlive
14181420
) {
1419-
instance.a && queuePostRenderEffect(instance.a, parentSuspense)
1421+
if (parentIsAsyncWrapperAndShouldKeepAlive) {
1422+
// #7276 - parent.a contains all the hooks of the descendants
1423+
parent.a && queuePostRenderEffect(parent.a, parentSuspense)
1424+
} else {
1425+
instance.a && queuePostRenderEffect(instance.a, parentSuspense)
1426+
}
14201427
if (
14211428
__COMPAT__ &&
14221429
isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)

0 commit comments

Comments
 (0)