Skip to content

Commit

Permalink
Fix chunk buffering for server components (#34474)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding authored Feb 17, 2022
1 parent 74fa4d4 commit d4d79b2
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1764,19 +1764,28 @@ function createInlineDataStream(

if (!dataStreamFinished && dataStream) {
const dataStreamReader = dataStream.getReader()
dataStreamFinished = (async () => {
try {
while (true) {
const { done, value } = await dataStreamReader.read()
if (done) {
return

// 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
// the safe timing to pipe the data stream, this extra tick is
// necessary.
dataStreamFinished = new Promise((res) =>
setTimeout(async () => {
try {
while (true) {
const { done, value } = await dataStreamReader.read()
if (done) {
return res()
}
controller.enqueue(value)
}
controller.enqueue(value)
} catch (err) {
controller.error(err)
}
} catch (err) {
controller.error(err)
}
})()
res()
}, 0)
)
}
},
flush() {
Expand Down

0 comments on commit d4d79b2

Please sign in to comment.