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(
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))
......@@ -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
......
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