Unverified Commit 3fb72dc6 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Refactor code (#64429)

Small code refactoring to make the server do fewer work.

Closes NEXT-3096
parent fc7f62dc
......@@ -10,11 +10,12 @@ export function wrapClientComponentLoader(ComponentMod: any) {
return {
require: (...args: any[]) => {
const startTime = performance.now()
if (clientComponentLoadStart === 0) {
clientComponentLoadStart = performance.now()
clientComponentLoadStart = startTime
}
const startTime = performance.now()
try {
clientComponentLoadCount += 1
return ComponentMod.__next_app__.require(...args)
......
......@@ -130,6 +130,7 @@ export function createBufferedTransformStream(): TransformStream<
try {
const chunk = new Uint8Array(bufferByteLength)
let copiedBytes = 0
for (let i = 0; i < bufferedChunks.length; i++) {
const bufferedChunk = bufferedChunks[i]
chunk.set(bufferedChunk, copiedBytes)
......@@ -376,31 +377,29 @@ function createMergedTransformStream(
})
}
const CLOSE_TAG = '</body></html>'
/**
* This transform stream moves the suffix to the end of the stream, so results
* like `</body></html><script>...</script>` will be transformed to
* `<script>...</script></body></html>`.
*/
function createMoveSuffixStream(
suffix: string
): TransformStream<Uint8Array, Uint8Array> {
function createMoveSuffixStream(): TransformStream<Uint8Array, Uint8Array> {
let foundSuffix = false
const encodedSuffix = encoder.encode(suffix)
return new TransformStream({
transform(chunk, controller) {
if (foundSuffix) {
return controller.enqueue(chunk)
}
const index = indexOfUint8Array(chunk, encodedSuffix)
const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.BODY_AND_HTML)
if (index > -1) {
foundSuffix = true
// If the whole chunk is the suffix, then don't write anything, it will
// be written in the flush.
if (chunk.length === suffix.length) {
if (chunk.length === ENCODED_TAGS.CLOSED.BODY_AND_HTML.length) {
return
}
......@@ -410,9 +409,11 @@ function createMoveSuffixStream(
// In the case where the suffix is in the middle of the chunk, we need
// to split the chunk into two parts.
if (chunk.length > suffix.length + index) {
if (chunk.length > ENCODED_TAGS.CLOSED.BODY_AND_HTML.length + index) {
// Write out the part after the suffix.
const after = chunk.slice(index + suffix.length)
const after = chunk.slice(
index + ENCODED_TAGS.CLOSED.BODY_AND_HTML.length
)
controller.enqueue(after)
}
} else {
......@@ -422,7 +423,7 @@ function createMoveSuffixStream(
flush(controller) {
// Even if we didn't find the suffix, the HTML is not valid if we don't
// add it, so insert it at the end.
controller.enqueue(encodedSuffix)
controller.enqueue(ENCODED_TAGS.CLOSED.BODY_AND_HTML)
},
})
}
......@@ -542,10 +543,8 @@ export async function continueFizzStream(
validateRootLayout,
}: ContinueStreamOptions
): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
// Suffix itself might contain close tags at the end, so we need to split it.
const suffixUnclosed = suffix ? suffix.split(closeTag, 1)[0] : null
const suffixUnclosed = suffix ? suffix.split(CLOSE_TAG, 1)[0] : null
// If we're generating static HTML and there's an `allReady` promise on the
// stream, we need to wait for it to resolve before continuing.
......@@ -574,7 +573,7 @@ export async function continueFizzStream(
validateRootLayout ? createRootLayoutValidatorStream() : null,
// Close tags should always be deferred to the end
createMoveSuffixStream(closeTag),
createMoveSuffixStream(),
// Special head insertions
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
......@@ -612,8 +611,6 @@ export async function continueStaticPrerender(
prerenderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream, getServerInsertedHTML }: ContinueStaticPrerenderOptions
) {
const closeTag = '</body></html>'
return (
prerenderStream
// Buffer everything to avoid flushing too frequently
......@@ -623,7 +620,7 @@ export async function continueStaticPrerender(
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream(closeTag))
.pipeThrough(createMoveSuffixStream())
)
}
......@@ -636,8 +633,6 @@ export async function continueDynamicHTMLResume(
renderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream, getServerInsertedHTML }: ContinueResumeOptions
) {
const closeTag = '</body></html>'
return (
renderStream
// Buffer everything to avoid flushing too frequently
......@@ -647,7 +642,7 @@ export async function continueDynamicHTMLResume(
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream(closeTag))
.pipeThrough(createMoveSuffixStream())
)
}
......@@ -659,13 +654,11 @@ export async function continueDynamicDataResume(
renderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream }: ContinueDynamicDataResumeOptions
) {
const closeTag = '</body></html>'
return (
renderStream
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream(closeTag))
.pipeThrough(createMoveSuffixStream())
)
}
const BOT_UA_RE =
/Googlebot|Mediapartners-Google|AdsBot-Google|googleweblight|Storebot-Google|Google-PageRenderer|Bingbot|BingPreview|Slurp|DuckDuckBot|baiduspider|yandex|sogou|LinkedInBot|bitlybot|tumblr|vkShare|quora link preview|facebookexternalhit|facebookcatalog|Twitterbot|applebot|redditbot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|ia_archiver/i
export function isBot(userAgent: string): boolean {
return /Googlebot|Mediapartners-Google|AdsBot-Google|googleweblight|Storebot-Google|Google-PageRenderer|Bingbot|BingPreview|Slurp|DuckDuckBot|baiduspider|yandex|sogou|LinkedInBot|bitlybot|tumblr|vkShare|quora link preview|facebookexternalhit|facebookcatalog|Twitterbot|applebot|redditbot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|ia_archiver/i.test(
userAgent
)
return BOT_UA_RE.test(userAgent)
}
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