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

Fix chunk buffering for server components #34474

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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