Unverified Commit d2838ce3 authored by Ethan Arrowood's avatar Ethan Arrowood Committed by GitHub

Simplify `createRootLayoutValidatorStream` (#63484)

This PR is a follow up to #63427 and simplifies the
`createRootLayoutValidatorStream` function to check each chunk
individually instead of combining all of them into one. This should
improve performance

Closes NEXT-2868
parent d4f5368f
...@@ -439,38 +439,26 @@ export function createRootLayoutValidatorStream(): TransformStream< ...@@ -439,38 +439,26 @@ export function createRootLayoutValidatorStream(): TransformStream<
> { > {
let foundHtml = false let foundHtml = false
let foundBody = false let foundBody = false
let chunks: Uint8Array[] = []
let size = 0
return new TransformStream({ return new TransformStream({
async transform(chunk, controller) { async transform(chunk, controller) {
chunks.push(chunk) // Peek into the streamed chunk to see if the tags are present.
size += chunk.length if (
controller.enqueue(chunk) !foundHtml &&
}, indexOfUint8Array(chunk, ENCODED_TAGS.OPENING.HTML) > -1
flush(controller) { ) {
const content = new Uint8Array(size) foundHtml = true
let offset = 0
for (const chunk of chunks) {
content.set(chunk, offset)
offset += chunk.length
} }
// Peek into the streamed chunk to see if the tags are present. if (
if (!foundHtml || !foundBody) { !foundBody &&
if ( indexOfUint8Array(chunk, ENCODED_TAGS.OPENING.BODY) > -1
!foundHtml && ) {
indexOfUint8Array(content, ENCODED_TAGS.OPENING.HTML) > -1 foundBody = true
) {
foundHtml = true
}
if (
!foundBody &&
indexOfUint8Array(content, ENCODED_TAGS.OPENING.BODY) > -1
) {
foundBody = true
}
} }
controller.enqueue(chunk)
},
flush(controller) {
const missingTags: typeof window.__next_root_layout_missing_tags = [] const missingTags: typeof window.__next_root_layout_missing_tags = []
if (!foundHtml) missingTags.push('html') if (!foundHtml) missingTags.push('html')
if (!foundBody) missingTags.push('body') if (!foundBody) missingTags.push('body')
......
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