Content-Length: 619992 | pFad | http://github.com/classicvalues/next.js/commit/3fb72dc6580e551979ab6657c0ed0787fc5bf565

13 Refactor code (#64429) · classicvalues/next.js@3fb72dc · GitHub
Skip to content

Commit 3fb72dc

Browse files
authored
Refactor code (vercel#64429)
Small code refactoring to make the server do fewer work. Closes NEXT-3096
1 parent fc7f62d commit 3fb72dc

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

packages/next/src/server/client-component-renderer-logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ export function wrapClientComponentLoader(ComponentMod: any) {
1010

1111
return {
1212
require: (...args: any[]) => {
13+
const startTime = performance.now()
14+
1315
if (clientComponentLoadStart === 0) {
14-
clientComponentLoadStart = performance.now()
16+
clientComponentLoadStart = startTime
1517
}
1618

17-
const startTime = performance.now()
1819
try {
1920
clientComponentLoadCount += 1
2021
return ComponentMod.__next_app__.require(...args)

packages/next/src/server/stream-utils/node-web-streams-helper.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export function createBufferedTransformStream(): TransformStream<
130130
try {
131131
const chunk = new Uint8Array(bufferByteLength)
132132
let copiedBytes = 0
133+
133134
for (let i = 0; i < bufferedChunks.length; i++) {
134135
const bufferedChunk = bufferedChunks[i]
135136
chunk.set(bufferedChunk, copiedBytes)
@@ -376,31 +377,29 @@ function createMergedTransformStream(
376377
})
377378
}
378379

380+
const CLOSE_TAG = '</body></html>'
381+
379382
/**
380383
* This transform stream moves the suffix to the end of the stream, so results
381384
* like `</body></html><script>...</script>` will be transformed to
382385
* `<script>...</script></body></html>`.
383386
*/
384-
function createMoveSuffixStream(
385-
suffix: string
386-
): TransformStream<Uint8Array, Uint8Array> {
387+
function createMoveSuffixStream(): TransformStream<Uint8Array, Uint8Array> {
387388
let foundSuffix = false
388389

389-
const encodedSuffix = encoder.encode(suffix)
390-
391390
return new TransformStream({
392391
transform(chunk, controller) {
393392
if (foundSuffix) {
394393
return controller.enqueue(chunk)
395394
}
396395

397-
const index = indexOfUint8Array(chunk, encodedSuffix)
396+
const index = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.BODY_AND_HTML)
398397
if (index > -1) {
399398
foundSuffix = true
400399

401400
// If the whole chunk is the suffix, then don't write anything, it will
402401
// be written in the flush.
403-
if (chunk.length === suffix.length) {
402+
if (chunk.length === ENCODED_TAGS.CLOSED.BODY_AND_HTML.length) {
404403
return
405404
}
406405

@@ -410,9 +409,11 @@ function createMoveSuffixStream(
410409

411410
// In the case where the suffix is in the middle of the chunk, we need
412411
// to split the chunk into two parts.
413-
if (chunk.length > suffix.length + index) {
412+
if (chunk.length > ENCODED_TAGS.CLOSED.BODY_AND_HTML.length + index) {
414413
// Write out the part after the suffix.
415-
const after = chunk.slice(index + suffix.length)
414+
const after = chunk.slice(
415+
index + ENCODED_TAGS.CLOSED.BODY_AND_HTML.length
416+
)
416417
controller.enqueue(after)
417418
}
418419
} else {
@@ -422,7 +423,7 @@ function createMoveSuffixStream(
422423
flush(controller) {
423424
// Even if we didn't find the suffix, the HTML is not valid if we don't
424425
// add it, so insert it at the end.
425-
controller.enqueue(encodedSuffix)
426+
controller.enqueue(ENCODED_TAGS.CLOSED.BODY_AND_HTML)
426427
},
427428
})
428429
}
@@ -542,10 +543,8 @@ export async function continueFizzStream(
542543
validateRootLayout,
543544
}: ContinueStreamOptions
544545
): Promise<ReadableStream<Uint8Array>> {
545-
const closeTag = '</body></html>'
546-
547546
// Suffix itself might contain close tags at the end, so we need to split it.
548-
const suffixUnclosed = suffix ? suffix.split(closeTag, 1)[0] : null
547+
const suffixUnclosed = suffix ? suffix.split(CLOSE_TAG, 1)[0] : null
549548

550549
// If we're generating static HTML and there's an `allReady` promise on the
551550
// stream, we need to wait for it to resolve before continuing.
@@ -574,7 +573,7 @@ export async function continueFizzStream(
574573
validateRootLayout ? createRootLayoutValidatorStream() : null,
575574

576575
// Close tags should always be deferred to the end
577-
createMoveSuffixStream(closeTag),
576+
createMoveSuffixStream(),
578577

579578
// Special head insertions
580579
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
@@ -612,8 +611,6 @@ export async function continueStaticPrerender(
612611
prerenderStream: ReadableStream<Uint8Array>,
613612
{ inlinedDataStream, getServerInsertedHTML }: ContinueStaticPrerenderOptions
614613
) {
615-
const closeTag = '</body></html>'
616-
617614
return (
618615
prerenderStream
619616
// Buffer everything to avoid flushing too frequently
@@ -623,7 +620,7 @@ export async function continueStaticPrerender(
623620
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
624621
.pipeThrough(createMergedTransformStream(inlinedDataStream))
625622
// Close tags should always be deferred to the end
626-
.pipeThrough(createMoveSuffixStream(closeTag))
623+
.pipeThrough(createMoveSuffixStream())
627624
)
628625
}
629626

@@ -636,8 +633,6 @@ export async function continueDynamicHTMLResume(
636633
renderStream: ReadableStream<Uint8Array>,
637634
{ inlinedDataStream, getServerInsertedHTML }: ContinueResumeOptions
638635
) {
639-
const closeTag = '</body></html>'
640-
641636
return (
642637
renderStream
643638
// Buffer everything to avoid flushing too frequently
@@ -647,7 +642,7 @@ export async function continueDynamicHTMLResume(
647642
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
648643
.pipeThrough(createMergedTransformStream(inlinedDataStream))
649644
// Close tags should always be deferred to the end
650-
.pipeThrough(createMoveSuffixStream(closeTag))
645+
.pipeThrough(createMoveSuffixStream())
651646
)
652647
}
653648

@@ -659,13 +654,11 @@ export async function continueDynamicDataResume(
659654
renderStream: ReadableStream<Uint8Array>,
660655
{ inlinedDataStream }: ContinueDynamicDataResumeOptions
661656
) {
662-
const closeTag = '</body></html>'
663-
664657
return (
665658
renderStream
666659
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
667660
.pipeThrough(createMergedTransformStream(inlinedDataStream))
668661
// Close tags should always be deferred to the end
669-
.pipeThrough(createMoveSuffixStream(closeTag))
662+
.pipeThrough(createMoveSuffixStream())
670663
)
671664
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
const BOT_UA_RE =
2+
/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
3+
14
export function isBot(userAgent: string): boolean {
2-
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(
3-
userAgent
4-
)
5+
return BOT_UA_RE.test(userAgent)
56
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/classicvalues/next.js/commit/3fb72dc6580e551979ab6657c0ed0787fc5bf565

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy