Unverified Commit ccd6c4b2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix resume stream handling (#58159)

Ensures we don't await un-necessarily.
parent 7e368c2e
...@@ -611,12 +611,11 @@ async function renderToHTMLOrFlightImpl( ...@@ -611,12 +611,11 @@ async function renderToHTMLOrFlightImpl(
const hasPostponed = typeof renderOpts.postponed === 'string' const hasPostponed = typeof renderOpts.postponed === 'string'
let stringifiedFlightPayloadPromise = let stringifiedFlightPayloadPromise = isStaticGeneration
isStaticGeneration || hasPostponed ? generateFlight(ctx)
? generateFlight(ctx) .then((renderResult) => renderResult.toUnchunkedString(true))
.then((renderResult) => renderResult.toUnchunkedString(true)) .catch(() => null)
.catch(() => null) : Promise.resolve(null)
: Promise.resolve(null)
// Get the nonce from the incoming request if it has one. // Get the nonce from the incoming request if it has one.
const csp = req.headers['content-security-policy'] const csp = req.headers['content-security-policy']
......
import { Suspense } from 'react'
import { cookies } from 'next/headers'
export default function Page() {
return (
<>
<p>delayed page</p>
<Suspense fallback={'loading 1...'}>
<Time />
</Suspense>
<Suspense fallback={'loading 2...'}>
<Random />
</Suspense>
</>
)
}
async function Time() {
cookies()
await new Promise((resolve) => setTimeout(resolve, 1000))
return <p id="time">{Date.now()}</p>
}
async function Random() {
cookies()
await new Promise((resolve) => setTimeout(resolve, 4000))
return <p id="random">{Math.random()}</p>
}
...@@ -15,6 +15,7 @@ describe('required server files app router', () => { ...@@ -15,6 +15,7 @@ describe('required server files app router', () => {
let next: NextInstance let next: NextInstance
let server let server
let appPort let appPort
let delayedPostpone
const setupNext = async ({ const setupNext = async ({
nextEnv, nextEnv,
...@@ -50,6 +51,9 @@ describe('required server files app router', () => { ...@@ -50,6 +51,9 @@ describe('required server files app router', () => {
}) })
await next.stop() await next.stop()
delayedPostpone = (await next.readJSON('.next/server/app/delayed.meta'))
.postponed
await fs.move( await fs.move(
join(next.testDir, '.next/standalone'), join(next.testDir, '.next/standalone'),
join(next.testDir, 'standalone') join(next.testDir, 'standalone')
...@@ -106,6 +110,37 @@ describe('required server files app router', () => { ...@@ -106,6 +110,37 @@ describe('required server files app router', () => {
expect(next.cliOutput).not.toContain('ERR_INVALID_URL') expect(next.cliOutput).not.toContain('ERR_INVALID_URL')
}) })
it('should properly stream resume', async () => {
const res = await fetchViaHTTP(appPort, '/delayed', undefined, {
headers: {
'x-matched-path': '/_next/postponed/resume/delayed',
},
method: 'POST',
body: delayedPostpone,
})
let chunks = []
for await (const chunk of res.body) {
chunks.push({
time: Date.now(),
chunk: chunk.toString(),
})
}
const firstSuspense = chunks.find((item) => item.chunk.includes('time'))
const secondSuspense = chunks.find((item) => item.chunk.includes('random'))
console.log({
firstSuspense,
secondSuspense,
})
expect(secondSuspense.time - firstSuspense.time).toBeGreaterThanOrEqual(
2 * 1000
)
})
it('should properly handle prerender for bot request', async () => { it('should properly handle prerender for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/first', undefined, { const res = await fetchViaHTTP(appPort, '/isr/first', undefined, {
headers: { headers: {
......
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