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

Delay prefix flushing #35170

Merged
merged 1 commit into from Mar 9, 2022
Merged
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
17 changes: 13 additions & 4 deletions packages/next/server/render.tsx
Expand Up @@ -1820,17 +1820,25 @@ function createPrefixStream(
prefix: string
): TransformStream<Uint8Array, Uint8Array> {
let prefixFlushed = false
let prefixPrefixFlushFinished: Promise<void> | null = null
return createTransformStream({
transform(chunk, controller) {
controller.enqueue(chunk)
if (!prefixFlushed && prefix) {
prefixFlushed = true
controller.enqueue(chunk)
controller.enqueue(encodeText(prefix))
} else {
controller.enqueue(chunk)
prefixPrefixFlushFinished = new Promise((res) => {
// NOTE: streaming flush
// Enqueue prefix part before the major chunks are enqueued so that
// prefix won't be flushed too early to interrupt the data stream
setTimeout(() => {
controller.enqueue(encodeText(prefix))
res()
})
})
}
},
flush(controller) {
if (prefixPrefixFlushFinished) return prefixPrefixFlushFinished
if (!prefixFlushed && prefix) {
prefixFlushed = true
controller.enqueue(encodeText(prefix))
Expand All @@ -1850,6 +1858,7 @@ function createInlineDataStream(
if (!dataStreamFinished) {
const dataStreamReader = dataStream.getReader()

// NOTE: streaming flush
// We are buffering here for the inlined data stream because the
// "shell" stream might be chunkenized again by the underlying stream
// implementation, e.g. with a specific high-water mark. To ensure it's
Expand Down