Skip to content

Commit 9c13f3a

Browse files
author
Simon Johansson
committed
fix(ssr): reset current instance if setting up options component errors
close #7733
1 parent a0e7dc3 commit 9c13f3a

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
@@ -840,9 +840,12 @@ export function finishComponentSetup(
840840
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
841841
setCurrentInstance(instance)
842842
pauseTracking()
843-
applyOptions(instance)
844-
resetTracking()
845-
unsetCurrentInstance()
843+
try {
844+
applyOptions(instance)
845+
resetTracking()
846+
} finally {
847+
unsetCurrentInstance()
848+
}
846849
}
847850

848851
// 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
@@ -794,6 +794,47 @@ function testRender(type: string, render: typeof renderToString) {
794794
} catch {}
795795
expect(getCurrentInstance()).toBe(prev)
796796
})
797+
798+
// #7733
799+
test('reset current instance after error in data', async () => {
800+
const prev = getCurrentInstance()
801+
expect(prev).toBe(null)
802+
try {
803+
await render(
804+
createApp({
805+
data() {
806+
throw new Error()
807+
},
808+
template: `<div>hello</div>`
809+
})
810+
)
811+
} catch {}
812+
expect(getCurrentInstance()).toBe(null)
813+
})
814+
})
815+
816+
// #7733
817+
test('reset current instance after error in errorCaptured', async () => {
818+
const prev = getCurrentInstance()
819+
820+
expect(prev).toBe(null)
821+
try {
822+
await render(
823+
createApp({
824+
errorCaptured() {
825+
throw new Error()
826+
},
827+
template: `<div>hello</div>`,
828+
created() {
829+
throw new Error()
830+
}
831+
})
832+
)
833+
} catch {}
834+
expect(
835+
'Unhandled error during execution of created hook'
836+
).toHaveBeenWarned()
837+
expect(getCurrentInstance()).toBe(null)
797838
})
798839

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

0 commit comments

Comments
 (0)