Skip to content

Commit da101a4

Browse files
authored
fix(suspense): drop async setup errors from unmounted components (#16350)
close #16349
1 parent 096dc12 commit da101a4

2 files changed

Lines changed: 28 additions & 47 deletions

File tree

‎packages/runtime-core/__tests__/components/Suspense.spec.ts‎

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4645,23 +4645,17 @@ describe('Suspense', () => {
46454645
expect(serializeInner(root)).toBe('<div><span>dep</span><!--v-if--></div>')
46464646
})
46474647

4648-
test.each([
4649-
{ unmount: 'none', captureOuter: true },
4650-
{ unmount: 'boundary', captureOuter: true },
4651-
{ unmount: 'boundary', captureOuter: false },
4652-
{ unmount: 'app', captureOuter: true },
4653-
{ unmount: 'branch', captureOuter: true },
4654-
{ unmount: 'branch', captureOuter: false },
4655-
])(
4656-
'async setup rejection (unmount: $unmount, capture outer: $captureOuter)',
4657-
async ({ unmount, captureOuter }) => {
4648+
// #16349
4649+
test.each(['none', 'boundary', 'app', 'branch'])(
4650+
'async setup rejection (unmount: %s)',
4651+
async unmount => {
46584652
const error = new Error('request failed')
46594653
let reject!: (error: Error) => void
46604654
const pending = new Promise<void>((_, rejectPending) => {
46614655
reject = rejectPending
46624656
})
46634657
const innerHandler = vi.fn(() => false)
4664-
const outerHandler = vi.fn(() => (captureOuter ? false : undefined))
4658+
const outerHandler = vi.fn(() => false)
46654659
const appHandler = vi.fn()
46664660
const renderAsync = vi.fn(() => h('div', 'ready'))
46674661
const show = ref(true)
@@ -4719,33 +4713,14 @@ describe('Suspense', () => {
47194713
expect.any(Object),
47204714
'setup function',
47214715
)
4722-
expect(outerHandler).not.toHaveBeenCalled()
4723-
expect(appHandler).not.toHaveBeenCalled()
47244716
} else {
4717+
// the component is gone, and the outcome of its setup with it
47254718
expect(innerHandler).not.toHaveBeenCalled()
4726-
if (unmount === 'boundary' || unmount === 'branch') {
4727-
expect(outerHandler).toHaveBeenCalledTimes(1)
4728-
expect(outerHandler).toHaveBeenCalledWith(
4729-
error,
4730-
expect.any(Object),
4731-
'setup function',
4732-
)
4733-
} else {
4734-
expect(outerHandler).not.toHaveBeenCalled()
4735-
}
4736-
if (unmount === 'app' || !captureOuter) {
4737-
expect(appHandler).toHaveBeenCalledTimes(1)
4738-
expect(appHandler).toHaveBeenCalledWith(
4739-
error,
4740-
expect.any(Object),
4741-
'setup function',
4742-
)
4743-
} else {
4744-
expect(appHandler).not.toHaveBeenCalled()
4745-
}
47464719
expect(renderAsync).not.toHaveBeenCalled()
47474720
expect(root.innerHTML).toBe(unmount === 'app' ? '' : '<p>left</p>')
47484721
}
4722+
expect(outerHandler).not.toHaveBeenCalled()
4723+
expect(appHandler).not.toHaveBeenCalled()
47494724
if (unmount !== 'app') app.unmount()
47504725
},
47514726
)
@@ -4835,18 +4810,20 @@ describe('Suspense', () => {
48354810
await new Promise(r => setTimeout(r))
48364811

48374812
expect(innerHandler).not.toHaveBeenCalled()
4838-
expect(outerHandler).toHaveBeenCalledTimes(1)
4839-
expect(outerHandler).toHaveBeenCalledWith(
4840-
error,
4841-
expect.any(Object),
4842-
source === 'loader'
4843-
? 'async component loader'
4844-
: source === 'watcher'
4845-
? 'watcher callback'
4846-
: source === 'teardown'
4847-
? 'beforeUnmount hook'
4848-
: 'setup function',
4849-
)
4813+
if (source === 'setup') {
4814+
expect(outerHandler).not.toHaveBeenCalled()
4815+
} else {
4816+
expect(outerHandler).toHaveBeenCalledTimes(1)
4817+
expect(outerHandler).toHaveBeenCalledWith(
4818+
error,
4819+
expect.any(Object),
4820+
source === 'loader'
4821+
? 'async component loader'
4822+
: source === 'watcher'
4823+
? 'watcher callback'
4824+
: 'beforeUnmount hook',
4825+
)
4826+
}
48504827
expect(appHandler).not.toHaveBeenCalled()
48514828
if (source === 'setup' || source === 'loader') {
48524829
expect(renderChild).not.toHaveBeenCalled()

‎packages/runtime-core/src/components/Suspense.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,12 @@ function createSuspenseBoundary(
887887
}
888888
instance
889889
.asyncDep!.catch(err => {
890-
// don't rethrow in dev: the dep must still be released below
891-
handleError(err, instance, ErrorCodes.SETUP_FUNCTION, false, true)
890+
// an unmounted component has no setup outcome to report. check the
891+
// scope: `isUnmounted` is deferred while the boundary is pending.
892+
if (instance.scope.active) {
893+
// don't rethrow in dev: the dep must still be released below
894+
handleError(err, instance, ErrorCodes.SETUP_FUNCTION, false, true)
895+
}
892896
// a failed setup renders nothing instead of its template
893897
return () => null
894898
})

0 commit comments

Comments
 (0)