Skip to content

Commit 1c049d9

Browse files
Simon Johanssonyyx990803
authored andcommitted
fix(ssr): reset current instance if setting up options component errors
close #7733
1 parent 372ec35 commit 1c049d9

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-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+
resetTracking()
909+
} finally {
910+
unsetCurrentInstance()
911+
}
909912
}
910913

911914
// warn missing template/render

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,47 @@ 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+
try {
821+
await render(
822+
createApp({
823+
errorCaptured() {
824+
throw new Error()
825+
},
826+
template: `<div>hello</div>`,
827+
created() {
828+
throw new Error()
829+
}
830+
})
831+
)
832+
} catch {}
833+
expect(
834+
'Unhandled error during execution of created hook'
835+
).toHaveBeenWarned()
836+
expect(getCurrentInstance()).toBe(null)
796837
})
797838

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

0 commit comments

Comments
 (0)