Unverified Commit e053b759 authored by Josh Story's avatar Josh Story Committed by GitHub

fix: handle shell errors when resuming Node streams (#94668)

The Node resume adapter returned its stream without observing whether
React produced a resumable HTML shell. When an empty prerendered shell
resumed into a redirect, React reported an onShellError, but Next.js
continued into Flight injection and waited forever for an HTML chunk
that would never arrive.

Wire the resume shell callbacks into a promise and await the result
after starting the pipe. Shell errors now propagate into the existing
app-render recovery path, matching the Web stream behavior and producing
a complete error document containing the redirect signal.
parent ef65efdf
...@@ -612,18 +612,29 @@ export async function resumeToFizzStream( ...@@ -612,18 +612,29 @@ export async function resumeToFizzStream(
const run: <T>(fn: () => T) => T = runInContext ?? ((fn) => fn()) const run: <T>(fn: () => T) => T = runInContext ?? ((fn) => fn())
const pt = new PassThrough() const pt = new PassThrough()
const shellReady = new DetachedPromise<void>()
const allReady = new DetachedPromise<void>() const allReady = new DetachedPromise<void>()
const pipeable = await run(() => const pipeable = await run(() =>
resumeToPipeableStream(element, postponedState, { resumeToPipeableStream(element, postponedState, {
...streamOptions, ...streamOptions,
onShellReady() {
streamOptions?.onShellReady?.()
shellReady.resolve()
},
onShellError(error: unknown) {
streamOptions?.onShellError?.(error)
shellReady.reject(error)
},
onAllReady() { onAllReady() {
streamOptions?.onAllReady?.() streamOptions?.onAllReady?.()
allReady.resolve() allReady.resolve()
}, },
}) })
) )
pipeable.pipe(pt) pipeable.pipe(pt)
await shellReady.promise
return { return {
stream: pt, stream: pt,
......
import { ReactNode } from 'react'
import { cookies } from 'next/headers'
export const instant = false
async function LayoutContent({ children }: { children: ReactNode }) {
await cookies()
return children
}
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body>
<LayoutContent>{children}</LayoutContent>
</body>
</html>
)
}
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
export default async function LoginPage() {
const session = (await cookies()).get('session')
if (session) {
redirect('/search')
}
return <form id="login">Log in</form>
}
export default function SearchPage() {
return <p>Search</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('empty shell redirect resume', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should build an empty shell with postponed state', async () => {
expect(await next.readFile('.next/server/app/login.html')).toBe('')
const metadata = JSON.parse(
await next.readFile('.next/server/app/login.meta')
)
expect(metadata.postponed).toBeTruthy()
})
it('should resume an empty shell with a redirect', async () => {
const response = await fetch(`${next.url}/login`, {
headers: {
cookie: 'session=valid',
},
signal: AbortSignal.timeout(3000),
})
const html = await response.text()
expect(response.status).toBe(200)
expect(html).toContain('id="__next_error__"')
expect(html).toContain('NEXT_REDIRECT;replace;/search;307;')
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
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