Skip to content

Commit

Permalink
fix(runtime-core): properly update async component nested in KeepAlive (
Browse files Browse the repository at this point in the history
#11917)

close #11916
  • Loading branch information
edison1105 authored Sep 13, 2024
1 parent 0e7bc71 commit 7fe6c79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
33 changes: 33 additions & 0 deletions packages/runtime-core/__tests__/apiAsyncComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,37 @@ describe('api: defineAsyncComponent', () => {
await timeout()
expect(serializeInner(root)).toBe('Bar')
})

// 11916
test('with KeepAlive + include', async () => {
const spy = vi.fn()
let resolve: (comp: Component) => void

const Foo = defineAsyncComponent(
() =>
new Promise(r => {
resolve = r as any
}),
)

const root = nodeOps.createElement('div')
const app = createApp({
render: () => h(KeepAlive, { include: 'Foo' }, [h(Foo)]),
})

app.mount(root)
await nextTick()

resolve!({
name: 'Foo',
setup() {
onActivated(spy)
return () => 'Foo'
},
})

await timeout()
expect(serializeInner(root)).toBe('Foo')
expect(spy).toBeCalledTimes(1)
})
})
3 changes: 1 addition & 2 deletions packages/runtime-core/src/apiAsyncComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { warn } from './warning'
import { ref } from '@vue/reactivity'
import { ErrorCodes, handleError } from './errorHandling'
import { isKeepAlive } from './components/KeepAlive'
import { queueJob } from './scheduler'
import { markAsyncBoundary } from './helpers/useId'
import { type HydrationStrategy, forEachElement } from './hydrationStrategies'

Expand Down Expand Up @@ -210,7 +209,7 @@ export function defineAsyncComponent<
if (instance.parent && isKeepAlive(instance.parent.vnode)) {
// parent is keep-alive, force update so the loaded component's
// name is taken into account
queueJob(instance.parent.update)
instance.parent.update()
}
})
.catch(err => {
Expand Down

0 comments on commit 7fe6c79

Please sign in to comment.