Unverified Commit a0e51781 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

fix: Error: NEXT_REDIRECT crashing server in prod (#42793)

Fixes: #42587

## 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)
parent 0f8b3d6c
...@@ -59,6 +59,24 @@ function preloadComponent(Component: any, props: any) { ...@@ -59,6 +59,24 @@ function preloadComponent(Component: any, props: any) {
} }
try { try {
let result = Component(props) let result = Component(props)
if (result && result.then) {
result = result
.then((res: any) => {
return { success: res }
})
.catch((err: Error) => {
return { error: err }
})
return async () => {
const res = await result
if (res.error) {
throw res.error
}
if (res.success) {
return res.success
}
}
}
return function () { return function () {
// We know what this component will render already. // We know what this component will render already.
return result return result
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
import path from 'path'
describe('async-component-preload', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'async-component-preload')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
})
})
afterAll(() => next.destroy())
it('should handle redirect in an async page', async () => {
const browser = await webdriver(next.url, '/')
expect(await browser.waitForElementByCss('#success').text()).toBe('Success')
})
})
export const revalidate = 0
export default async function RootLayout({ children }) {
await new Promise((resolve) => setTimeout(resolve, 0))
return (
<html lang="en">
<head />
<body>{children}</body>
</html>
)
}
import { redirect } from 'next/navigation'
export default async function Home() {
redirect('success')
return <h1>Home</h1>
}
export default function Success() {
return <p id="success">Success</p>
}
module.exports = {
experimental: {
appDir: true,
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment