Skip to content

Commit ce5b59d

Browse files
author
Hannes Bornö
authored
Remove unnecessary async function when preloading async components (vercel#42957)
The fix in vercel#42793 added an unnecessary step by returning a new async function when the result is a promise. We only have to make sure we don't get an unhandledRejection error if the promise rejects, React takes care of getting the data from the promise. Ref: [slack thread](https://vercel.slack.com/archives/C035J346QQL/p1668528003500329?thread_ts=1668434306.364449&cid=C035J346QQL) ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 58fa90f commit ce5b59d

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

packages/next/server/app-render.tsx

+6-17
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,12 @@ function preloadComponent(Component: any, props: any) {
5959
}
6060
try {
6161
let result = Component(props)
62-
if (result && result.then) {
63-
result = result
64-
.then((res: any) => {
65-
return { success: res }
66-
})
67-
.catch((err: Error) => {
68-
return { error: err }
69-
})
70-
return async () => {
71-
const res = await result
72-
if (res.error) {
73-
throw res.error
74-
}
75-
if (res.success) {
76-
return res.success
77-
}
78-
}
62+
if (result && typeof result.then === 'function') {
63+
// Catch promise rejections to prevent unhandledRejection errors
64+
result.then(
65+
() => {},
66+
() => {}
67+
)
7968
}
8069
return function () {
8170
// We know what this component will render already.

0 commit comments

Comments
 (0)