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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Error: NEXT_REDIRECT crashing server in prod #42793

Merged
merged 4 commits into from Nov 15, 2022
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
18 changes: 18 additions & 0 deletions packages/next/server/app-render.tsx
Expand Up @@ -59,6 +59,24 @@ function preloadComponent(Component: any, props: any) {
}
try {
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 () {
// We know what this component will render already.
return result
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/app-dir/async-component-preload.test.ts
@@ -0,0 +1,29 @@
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')
})
})
12 changes: 12 additions & 0 deletions test/e2e/app-dir/async-component-preload/app/layout.js
@@ -0,0 +1,12 @@
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>
)
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/async-component-preload/app/page.js
@@ -0,0 +1,6 @@
import { redirect } from 'next/navigation'

export default async function Home() {
redirect('success')
return <h1>Home</h1>
}
@@ -0,0 +1,3 @@
export default function Success() {
return <p id="success">Success</p>
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/async-component-preload/next.config.js
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
appDir: true,
},
}