Skip to content

Commit 020851e

Browse files
authored
fix(ssr): reset current instance if setting up options component errors (#7743)
close #7733
1 parent 372ec35 commit 020851e

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

packages/runtime-core/src/component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,12 @@ export function finishComponentSetup(
903903
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
904904
setCurrentInstance(instance)
905905
pauseTracking()
906-
applyOptions(instance)
907-
resetTracking()
908-
unsetCurrentInstance()
906+
try {
907+
applyOptions(instance)
908+
} finally {
909+
resetTracking()
910+
unsetCurrentInstance()
911+
}
909912
}
910913

911914
// warn missing template/render

packages/server-renderer/__tests__/render.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,50 @@ function testRender(type: string, render: typeof renderToString) {
793793
} catch {}
794794
expect(getCurrentInstance()).toBe(prev)
795795
})
796+
797+
// #7733
798+
test('reset current instance after error in data', async () => {
799+
const prev = getCurrentInstance()
800+
expect(prev).toBe(null)
801+
try {
802+
await render(
803+
createApp({
804+
data() {
805+
throw new Error()
806+
},
807+
template: `<div>hello</div>`
808+
})
809+
)
810+
} catch {}
811+
expect(getCurrentInstance()).toBe(null)
812+
})
813+
})
814+
815+
// #7733
816+
test('reset current instance after error in errorCaptured', async () => {
817+
const prev = getCurrentInstance()
818+
819+
expect(prev).toBe(null)
820+
821+
const Child = {
822+
created() {
823+
throw new Error()
824+
}
825+
}
826+
try {
827+
await render(
828+
createApp({
829+
errorCaptured() {
830+
throw new Error()
831+
},
832+
render: () => h(Child)
833+
})
834+
)
835+
} catch {}
836+
expect(
837+
'Unhandled error during execution of errorCaptured hook'
838+
).toHaveBeenWarned()
839+
expect(getCurrentInstance()).toBe(null)
796840
})
797841

798842
test('serverPrefetch', async () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy