Unverified Commit 55c063ea authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Delay prefix flushing (#35170)

Only happened with SSR without suspense case

Similar reason to #34474, the prefix (script parts) might be flushed during the render stream causing bad HTML. Use the same tricky to delay the prefix flushing

#### Expected
```html
<div>content</div> <!-- render stream --> 
<script>...</script> <!-- prefix --> 
```


#### Observed
```html
<!-- prefix choked the render stream --> 
<div <script>...</script> >content</div>
```


Test sample:
https://next-react-server-components-r5xocii9r-huozhi.vercel.app/ssr
parent 201f98e8
...@@ -1820,17 +1820,25 @@ function createPrefixStream( ...@@ -1820,17 +1820,25 @@ function createPrefixStream(
prefix: string prefix: string
): TransformStream<Uint8Array, Uint8Array> { ): TransformStream<Uint8Array, Uint8Array> {
let prefixFlushed = false let prefixFlushed = false
let prefixPrefixFlushFinished: Promise<void> | null = null
return createTransformStream({ return createTransformStream({
transform(chunk, controller) { transform(chunk, controller) {
controller.enqueue(chunk)
if (!prefixFlushed && prefix) { if (!prefixFlushed && prefix) {
prefixFlushed = true prefixFlushed = true
controller.enqueue(chunk) prefixPrefixFlushFinished = new Promise((res) => {
controller.enqueue(encodeText(prefix)) // NOTE: streaming flush
} else { // Enqueue prefix part before the major chunks are enqueued so that
controller.enqueue(chunk) // prefix won't be flushed too early to interrupt the data stream
setTimeout(() => {
controller.enqueue(encodeText(prefix))
res()
})
})
} }
}, },
flush(controller) { flush(controller) {
if (prefixPrefixFlushFinished) return prefixPrefixFlushFinished
if (!prefixFlushed && prefix) { if (!prefixFlushed && prefix) {
prefixFlushed = true prefixFlushed = true
controller.enqueue(encodeText(prefix)) controller.enqueue(encodeText(prefix))
...@@ -1850,6 +1858,7 @@ function createInlineDataStream( ...@@ -1850,6 +1858,7 @@ function createInlineDataStream(
if (!dataStreamFinished) { if (!dataStreamFinished) {
const dataStreamReader = dataStream.getReader() const dataStreamReader = dataStream.getReader()
// NOTE: streaming flush
// We are buffering here for the inlined data stream because the // We are buffering here for the inlined data stream because the
// "shell" stream might be chunkenized again by the underlying stream // "shell" stream might be chunkenized again by the underlying stream
// implementation, e.g. with a specific high-water mark. To ensure it's // implementation, e.g. with a specific high-water mark. To ensure it's
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment