Skip to content

Commit

Permalink
try to get inlinedDataStream working
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-Arrowood committed May 13, 2024
1 parent ebe1d38 commit 3ee51ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
71 changes: 36 additions & 35 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1328,45 +1328,46 @@ async function renderToHTMLOrFlightImpl(
},
})

if (
process.env.NEXT_RUNTIME === 'nodejs' &&
!(fizzStream instanceof ReadableStream)
) {
const { Readable } = require('node:stream')

fizzStream = Readable.toWeb(
fizzStream
) as ReadableStream<Uint8Array>
}

// TODO (@Ethan-Arrowood): Remove this when stream utilities support both stream types.
if (!(fizzStream instanceof ReadableStream)) {
throw new Error("Invariant: stream isn't a ReadableStream")
}

// if (
// process.env.NEXT_RUNTIME === 'nodejs' &&
// !(fizzStream instanceof ReadableStream)
// ) {
// const { Readable } = require('node:stream')

// fizzStream = Readable.toWeb(
// fizzStream
// ) as ReadableStream<Uint8Array>
// }

// // TODO (@Ethan-Arrowood): Remove this when stream utilities support both stream types.
// if (!(fizzStream instanceof ReadableStream)) {
// throw new Error("Invariant: stream isn't a ReadableStream")
// }

const resultStream = await continueFizzStream(fizzStream, {
inlinedDataStream: createInlinedDataReadableStream(
// This is intentionally using the readable datastream from the
// main render rather than the flight data from the error page
// render
dataStream,
nonce,
formState
),
isStaticGeneration,
getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills,
renderServerInsertedHTML,
serverCapturedErrors: [],
basePath: renderOpts.basePath,
}),
serverInsertedHTMLToHead: true,
validateRootLayout,
})
return {
// Returning the error that was thrown so it can be used to handle
// the response in the caller.
err,
stream: await continueFizzStream(fizzStream, {
inlinedDataStream: createInlinedDataReadableStream(
// This is intentionally using the readable datastream from the
// main render rather than the flight data from the error page
// render
dataStream,
nonce,
formState
),
isStaticGeneration,
getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills,
renderServerInsertedHTML,
serverCapturedErrors: [],
basePath: renderOpts.basePath,
}),
serverInsertedHTMLToHead: true,
validateRootLayout,
}),
stream: convertReadable(resultStream),
}
} catch (finalErr: any) {
if (
Expand Down
12 changes: 11 additions & 1 deletion packages/next/src/server/stream-utils/stream-utils.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ function createRootLayoutValidatorStream(): Transform {
})
}

function createPassThroughFromReadable(readable: Readable) {
return readable.pipe(new PassThrough())
}

export function continueFizzStream(
renderStream: Readable,
{
Expand All @@ -399,6 +403,12 @@ export function continueFizzStream(
suffix?: string
}
): Promise<Readable> {
// @ts-ignore
if (inlinedDataStream instanceof ReadableStream) {
// @ts-ignore
inlinedDataStream = Readable.fromWeb(inlinedDataStream)
}

const closeTag = '</body></html>'
const suffixUnclosed = suffix ? suffix.split(closeTag, 1)[0] : null

Expand All @@ -420,7 +430,7 @@ export function continueFizzStream(
}

if (inlinedDataStream) {
streams.push(inlinedDataStream)
streams.push(createPassThroughFromReadable(inlinedDataStream))
}

if (validateRootLayout) {
Expand Down

0 comments on commit 3ee51ad

Please sign in to comment.