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(Suspense): properly get anchor when mount fallback vnode #9770

Merged
merged 4 commits into from
Dec 8, 2023
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
66 changes: 66 additions & 0 deletions packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,72 @@ describe('Suspense', () => {
expect(calls).toEqual([`one mounted`, `one unmounted`, `two mounted`])
})

test('mount the fallback content is in the correct position', async () => {
const makeComp = (name: string, delay = 0) =>
defineAsyncComponent(
{
setup() {
return () => h('div', [name])
}
},
delay
)

const One = makeComp('one')
const Two = makeComp('two', 20)

const view = shallowRef(One)

const Comp = {
setup() {
return () =>
h('div', [
h(
Suspense,
{
timeout: 10
},
{
default: h(view.value),
fallback: h('div', 'fallback')
}
),
h('div', 'three')
])
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serializeInner(root)).toBe(
`<div><div>fallback</div><div>three</div></div>`
)

await deps[0]
await nextTick()
expect(serializeInner(root)).toBe(
`<div><div>one</div><div>three</div></div>`
)

view.value = Two
await nextTick()
expect(serializeInner(root)).toBe(
`<div><div>one</div><div>three</div></div>`
)

await new Promise(r => setTimeout(r, 10))
await nextTick()
expect(serializeInner(root)).toBe(
`<div><div>fallback</div><div>three</div></div>`
)

await deps[1]
await nextTick()
expect(serializeInner(root)).toBe(
`<div><div>two</div><div>three</div></div>`
)
})

// #2214
// Since suspense renders its own root like a component, it should not patch
// its content in optimized mode.
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ function createSuspenseBoundary(
// invoke @fallback event
triggerEvent(vnode, 'onFallback')

const anchor = next(activeBranch!)
const mountFallback = () => {
if (!suspense.isInFallback) {
return
Expand All @@ -591,7 +592,7 @@ function createSuspenseBoundary(
null,
fallbackVNode,
container,
next(activeBranch!),
anchor,
parentComponent,
null, // fallback tree will not have suspense context
isSVG,
Expand Down