Skip to content

Commit 928c779

Browse files
authored
Resolve streaming data for flight requests (vercel#32010)
## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
1 parent 1442b58 commit 928c779

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

packages/next/server/render.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,13 @@ export async function renderToHTML(
10081008
serverComponentManifest
10091009
)
10101010
const reader = stream.getReader()
1011-
return new RenderResult((innerRes, next) => {
1011+
const piper: NodeWritablePiper = (innerRes, next) => {
10121012
bufferedReadFromReadableStream(reader, (val) => innerRes.write(val)).then(
10131013
() => next(),
10141014
(innerErr) => next(innerErr)
10151015
)
1016-
})
1016+
}
1017+
return new RenderResult(chainPipers([piper]))
10171018
}
10181019

10191020
// we preload the buildManifest for auto-export dynamic pages

test/integration/react-streaming-and-server-components/app/pages/index.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function Index({ header, router }) {
1919

2020
export function getServerSideProps({ req }) {
2121
const { headers } = req
22-
const header = headers[headerKey]
22+
const header = headers[headerKey] || ''
2323

2424
return {
2525
props: {

test/integration/react-streaming-and-server-components/test/index.test.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ async function nextDev(dir, port) {
8686
})
8787
}
8888

89+
async function resolveStreamResponse(response, onData) {
90+
let result = ''
91+
onData = onData || (() => {})
92+
await new Promise((resolve) => {
93+
response.body.on('data', (chunk) => {
94+
result += chunk.toString()
95+
onData(chunk.toString(), result)
96+
})
97+
98+
response.body.on('end', resolve)
99+
})
100+
return result
101+
}
102+
89103
describe('concurrentFeatures - basic', () => {
90104
it('should warn user for experimental risk with server components', async () => {
91105
const edgeRuntimeWarning =
@@ -296,24 +310,17 @@ async function runBasicTests(context, env) {
296310
it('should support streaming', async () => {
297311
await fetchViaHTTP(context.appPort, '/streaming', null, {}).then(
298312
async (response) => {
299-
let result = ''
300313
let gotFallback = false
301314
let gotData = false
302315

303-
await new Promise((resolve) => {
304-
response.body.on('data', (chunk) => {
305-
result += chunk.toString()
306-
307-
gotData = result.includes('next_streaming_data')
308-
if (!gotFallback) {
309-
gotFallback = result.includes('next_streaming_fallback')
310-
if (gotFallback) {
311-
expect(gotData).toBe(false)
312-
}
316+
await resolveStreamResponse(response, (_, result) => {
317+
gotData = result.includes('next_streaming_data')
318+
if (!gotFallback) {
319+
gotFallback = result.includes('next_streaming_fallback')
320+
if (gotFallback) {
321+
expect(gotData).toBe(false)
313322
}
314-
})
315-
316-
response.body.on('end', () => resolve())
323+
}
317324
})
318325

319326
expect(gotFallback).toBe(true)
@@ -327,6 +334,15 @@ async function runBasicTests(context, env) {
327334
expect(content).toMatchInlineSnapshot('"next_streaming_data"')
328335
})
329336

337+
it('should support streaming flight request', async () => {
338+
await fetchViaHTTP(context.appPort, '/?__flight__=1').then(
339+
async (response) => {
340+
const result = await resolveStreamResponse(response)
341+
expect(result).toContain('component:index.server')
342+
}
343+
)
344+
})
345+
330346
it('should support api routes', async () => {
331347
const res = await renderViaHTTP(context.appPort, '/api/ping')
332348
expect(res).toContain('pong')

0 commit comments

Comments
 (0)