Skip to content

Commit c97c8c4

Browse files
authored
fix(router-view): handle pending async component roots in onRouteRendered (#2856)
Fix #2851
1 parent faa8b9f commit c97c8c4

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎packages/router/src/RouterView.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
196196
// same flush (nested RouterView, onActivated) run after this one, and
197197
// the app container isn't set yet during the initial mount
198198
nextTick(() => {
199+
// a pending async component root has no el yet, only its
200+
// placeholder comment does
201+
while (!vnode.el && vnode.component) vnode = vnode.component.subTree
199202
if (
200203
// already called: unrelated re-render or failed navigation (the
201204
// route didn't change)
@@ -207,7 +210,7 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
207210
routeToDisplay.value !== route ||
208211
// still in a Suspense hidden container, e.g. KeepAlive activating
209212
// inside a pending Suspense, a later hook fires once it is moved
210-
(vnode.el as Node).getRootNode() !==
213+
(vnode.el as Node | null)?.getRootNode() !==
211214
(app._container as Node).getRootNode()
212215
) {
213216
return

‎packages/router/src/experimental/on-route-rendered.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,46 @@ describe('onRouteRendered', () => {
391391
)
392392
})
393393

394+
it('nested page rendering a pending async component as its root', async () => {
395+
const { promise: pending, resolve } = Promise.withResolvers<void>()
396+
const AsyncContent = defineComponent({
397+
async setup() {
398+
await pending
399+
return () => h('p', 'Async')
400+
},
401+
})
402+
const AsyncPage = defineComponent({
403+
setup() {
404+
trackNavigations()
405+
return () => h(AsyncContent)
406+
},
407+
})
408+
const { go, wrapper } = await setup(
409+
[
410+
{
411+
path: '/',
412+
component: { template: '<section><RouterView /></section>' },
413+
children: [
414+
{ path: '', component: staticPage('Home') },
415+
{ path: 'async', component: AsyncPage },
416+
],
417+
},
418+
],
419+
`<RouterView v-slot="{ Component }">
420+
<Suspense><component :is="Component" /></Suspense>
421+
</RouterView>`
422+
)
423+
await go('/')
424+
await go('/async')
425+
// the ancestor Suspense is resolved: same as an element root
426+
expect(events).toEqual(['settled:1:/async dom=""'])
427+
events = []
428+
resolve()
429+
await flushPromises()
430+
expect(wrapper.text()).toBe('Async')
431+
expect(events).toEqual([])
432+
})
433+
394434
it('Suspense keyed async component keeps the old branch', async () => {
395435
const pending: Array<PromiseWithResolvers<void>> = []
396436
const Async = defineComponent({

0 commit comments

Comments
 (0)