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) { ...@@ -10,11 +10,12 @@ export function wrapClientComponentLoader(ComponentMod: any) {
return { return {
require: (...args: any[]) => { require: (...args: any[]) => {
const startTime = performance.now()
if (clientComponentLoadStart === 0) { if (clientComponentLoadStart === 0) {
clientComponentLoadStart = performance.now() clientComponentLoadStart = startTime
} }
const startTime = performance.now()
try { try {
clientComponentLoadCount += 1 clientComponentLoadCount += 1
return ComponentMod.__next_app__.require(...args) return ComponentMod.__next_app__.require(...args)
......
...@@ -130,6 +130,7 @@ export function createBufferedTransformStream(): TransformStream< ...@@ -130,6 +130,7 @@ export function createBufferedTransformStream(): TransformStream<
try { try {
const chunk = new Uint8Array(bufferByteLength) const chunk = new Uint8Array(bufferByteLength)
let copiedBytes = 0 let copiedBytes = 0
for (let i = 0; i < bufferedChunks.length; i++) { for (let i = 0; i < bufferedChunks.length; i++) {
const bufferedChunk = bufferedChunks[i] const bufferedChunk = bufferedChunks[i]
chunk.set(bufferedChunk, copiedBytes) chunk.set(bufferedChunk, copiedBytes)
...@@ -376,31 +377,29 @@ function createMergedTransformStream( ...@@ -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 * This transform stream moves the suffix to the end of the stream, so results
* like `</body></html><script>...</script>` will be transformed to * like `</body></html><script>...</script>` will be transformed to
* `<script>...</script></body></html>`. * `<script>...</script></body></html>`.
*/ */
function createMoveSuffixStream( function createMoveSuffixStream(): TransformStream<Uint8Array, Uint8Array> {
suffix: string
): TransformStream<Uint8Array, Uint8Array> {
let foundSuffix = false let foundSuffix = false
const encodedSuffix = encoder.encode(suffix)
return new TransformStream({ return new TransformStream({
transform(chunk, controller) { transform(chunk, controller) {
if (foundSuffix) { if (foundSuffix) {
return controller.enqueue(chunk) return controller.enqueue(chunk)
} }
const index = indexOfUint8Array(chunk, encodedSuffix) const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.BODY_AND_HTML)
if (index > -1) { if (index > -1) {
foundSuffix = true foundSuffix = true
// If the whole chunk is the suffix, then don't write anything, it will // If the whole chunk is the suffix, then don't write anything, it will
// be written in the flush. // be written in the flush.
if (chunk.length === suffix.length) { if (chunk.length === ENCODED_TAGS.CLOSED.BODY_AND_HTML.length) {
return return
} }
...@@ -410,9 +409,11 @@ function createMoveSuffixStream( ...@@ -410,9 +409,11 @@ function createMoveSuffixStream(
// In the case where the suffix is in the middle of the chunk, we need // In the case where the suffix is in the middle of the chunk, we need
// to split the chunk into two parts. // 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. // 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) controller.enqueue(after)
} }
} else { } else {
...@@ -422,7 +423,7 @@ function createMoveSuffixStream( ...@@ -422,7 +423,7 @@ function createMoveSuffixStream(
flush(controller) { flush(controller) {
// Even if we didn't find the suffix, the HTML is not valid if we don't // 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. // 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( ...@@ -542,10 +543,8 @@ export async function continueFizzStream(
validateRootLayout, validateRootLayout,
}: ContinueStreamOptions }: ContinueStreamOptions
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
// Suffix itself might contain close tags at the end, so we need to split it. // 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 // 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. // stream, we need to wait for it to resolve before continuing.
...@@ -574,7 +573,7 @@ export async function continueFizzStream( ...@@ -574,7 +573,7 @@ export async function continueFizzStream(
validateRootLayout ? createRootLayoutValidatorStream() : null, validateRootLayout ? createRootLayoutValidatorStream() : null,
// Close tags should always be deferred to the end // Close tags should always be deferred to the end
createMoveSuffixStream(closeTag), createMoveSuffixStream(),
// Special head insertions // Special head insertions
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid // TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
...@@ -612,8 +611,6 @@ export async function continueStaticPrerender( ...@@ -612,8 +611,6 @@ export async function continueStaticPrerender(
prerenderStream: ReadableStream<Uint8Array>, prerenderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream, getServerInsertedHTML }: ContinueStaticPrerenderOptions { inlinedDataStream, getServerInsertedHTML }: ContinueStaticPrerenderOptions
) { ) {
const closeTag = '</body></html>'
return ( return (
prerenderStream prerenderStream
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
...@@ -623,7 +620,7 @@ export async function continueStaticPrerender( ...@@ -623,7 +620,7 @@ export async function continueStaticPrerender(
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream)) .pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end // Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream(closeTag)) .pipeThrough(createMoveSuffixStream())
) )
} }
...@@ -636,8 +633,6 @@ export async function continueDynamicHTMLResume( ...@@ -636,8 +633,6 @@ export async function continueDynamicHTMLResume(
renderStream: ReadableStream<Uint8Array>, renderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream, getServerInsertedHTML }: ContinueResumeOptions { inlinedDataStream, getServerInsertedHTML }: ContinueResumeOptions
) { ) {
const closeTag = '</body></html>'
return ( return (
renderStream renderStream
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
...@@ -647,7 +642,7 @@ export async function continueDynamicHTMLResume( ...@@ -647,7 +642,7 @@ export async function continueDynamicHTMLResume(
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream)) .pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end // Close tags should always be deferred to the end
.pipeThrough(createMoveSuffixStream(closeTag)) .pipeThrough(createMoveSuffixStream())
) )
} }
...@@ -659,13 +654,11 @@ export async function continueDynamicDataResume( ...@@ -659,13 +654,11 @@ export async function continueDynamicDataResume(
renderStream: ReadableStream<Uint8Array>, renderStream: ReadableStream<Uint8Array>,
{ inlinedDataStream }: ContinueDynamicDataResumeOptions { inlinedDataStream }: ContinueDynamicDataResumeOptions
) { ) {
const closeTag = '</body></html>'
return ( return (
renderStream renderStream
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
.pipeThrough(createMergedTransformStream(inlinedDataStream)) .pipeThrough(createMergedTransformStream(inlinedDataStream))
// Close tags should always be deferred to the end // 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 { 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( return BOT_UA_RE.test(userAgent)
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