Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): reset current instance if setting up options component errors (fix #7733) #7743

Merged
merged 2 commits into from May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/runtime-core/src/component.ts
Expand Up @@ -903,9 +903,12 @@ export function finishComponentSetup(
if (__FEATURE_OPTIONS_API__ && !(__COMPAT__ && skipOptions)) {
setCurrentInstance(instance)
pauseTracking()
applyOptions(instance)
resetTracking()
unsetCurrentInstance()
try {
applyOptions(instance)
} finally {
resetTracking()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small change - moved resetTracking() into finally block too.

unsetCurrentInstance()
}
}

// warn missing template/render
Expand Down
44 changes: 44 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Expand Up @@ -793,6 +793,50 @@ 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)

const Child = {
created() {
throw new Error()
}
}
try {
await render(
createApp({
errorCaptured() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errorCaptured only captures errors from descendants, so a separate child component is needed.

throw new Error()
},
render: () => h(Child)
})
)
} catch {}
expect(
'Unhandled error during execution of errorCaptured hook'
).toHaveBeenWarned()
expect(getCurrentInstance()).toBe(null)
})

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