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): display current branch if no fallback is provided #3991

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ describe('Suspense', () => {
<div v-if="errorMessage">{{ errorMessage }}</div>
<Suspense v-else>
<div>
<Async />
<Async />
</div>
<template #fallback>
<div>fallback</div>
Expand Down Expand Up @@ -983,6 +983,66 @@ describe('Suspense', () => {
expect(serializeInner(root)).toBe(`<div>foo<div>foo nested</div></div>`)
})

test('display previous branch when timeout + no fallback slot is provided', async () => {
const toggle = ref(false)
let resolve = () => {}
let promise: Promise<void>
function createPromise() {
promise = new Promise<void>(r => {
resolve = r
})

return promise
}

const Foo = {
async setup() {
await createPromise()
return () => h('div', ['foo'])
}
}

const onPending = jest.fn()
const onFallback = jest.fn()
const onResolve = jest.fn()

const Comp = {
setup() {
return () =>
h(
Suspense,
{ timeout: 0, onPending, onFallback, onResolve },
{
default: toggle.value ? h(Foo) : 'other'
}
)
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(serializeInner(root)).toBe(`other`)
expect(onPending).toHaveBeenCalledTimes(0)
expect(onFallback).toHaveBeenCalledTimes(0)
expect(onResolve).toHaveBeenCalledTimes(1)

toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe(`other`)
expect(onPending).toHaveBeenCalledTimes(1)
expect(onFallback).toHaveBeenCalledTimes(0)
expect(onResolve).toHaveBeenCalledTimes(1)

resolve()
await promise!
await nextTick()
await nextTick()
expect(serializeInner(root)).toBe(`<div>foo</div>`)
expect(onPending).toHaveBeenCalledTimes(1)
expect(onFallback).toHaveBeenCalledTimes(0)
expect(onResolve).toHaveBeenCalledTimes(2)
})

test('branch switch to 3rd branch before resolve', async () => {
const calls: string[] = []

Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
VNodeProps,
isSameVNodeType,
openBlock,
Comment,
closeBlock,
currentBlock,
createVNode
Expand Down Expand Up @@ -516,7 +517,8 @@ function createSuspenseBoundary(
},

fallback(fallbackVNode) {
if (!suspense.pendingBranch) {
// avoid displaying the fallback/emitting node if there isn't any
posva marked this conversation as resolved.
Show resolved Hide resolved
if (!suspense.pendingBranch || fallbackVNode.type === Comment) {
return
}

Expand Down