Skip to content

Commit

Permalink
fix(ssr): reset current instance if setting up options component errors
Browse files Browse the repository at this point in the history
close #7733
  • Loading branch information
Simon Johansson committed Feb 17, 2023
1 parent a0e7dc3 commit 9c13f3a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/runtime-core/src/component.ts
Expand Up @@ -840,9 +840,12 @@ export function finishComponentSetup(
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
setCurrentInstance(instance)
pauseTracking()
applyOptions(instance)
resetTracking()
unsetCurrentInstance()
try {
applyOptions(instance)
resetTracking()
} finally {
unsetCurrentInstance()
}
}

// warn missing template/render
Expand Down
41 changes: 41 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Expand Up @@ -794,6 +794,47 @@ function testRender(type: string, render: typeof renderToString) {
} catch {}
expect(getCurrentInstance()).toBe(prev)
})

// #7733
test('reset current instance after error in data', async () => {
const prev = getCurrentInstance()
expect(prev).toBe(null)
try {
await render(
createApp({
data() {
throw new Error()
},
template: `<div>hello</div>`
})
)
} catch {}
expect(getCurrentInstance()).toBe(null)
})
})

// #7733
test('reset current instance after error in errorCaptured', async () => {
const prev = getCurrentInstance()

expect(prev).toBe(null)
try {
await render(
createApp({
errorCaptured() {
throw new Error()
},
template: `<div>hello</div>`,
created() {
throw new Error()
}
})
)
} catch {}
expect(
'Unhandled error during execution of created hook'
).toHaveBeenWarned()
expect(getCurrentInstance()).toBe(null)
})

test('serverPrefetch', async () => {
Expand Down

0 comments on commit 9c13f3a

Please sign in to comment.