Unverified Commit 26ea9c63 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Always defer close tags to be the suffix of body stream (#50056)

Currently the suffix logic is for pages so nothing happens in app dir. This PR changes it to **always** apply the `createSuffixStream(closeTag)` transform, to defer `</body></html>` to the end of the stream.
fix #48372
fix NEXT-1200
parent f0d0811a
...@@ -296,7 +296,25 @@ export function createInlineDataStream( ...@@ -296,7 +296,25 @@ export function createInlineDataStream(
export function createSuffixStream( export function createSuffixStream(
suffix: string suffix: string
): TransformStream<Uint8Array, Uint8Array> { ): TransformStream<Uint8Array, Uint8Array> {
let foundSuffix = false
const textDecoder = new TextDecoder()
// Remove suffix from the stream, and enqueue it back in flush
return new TransformStream({ return new TransformStream({
transform(chunk, controller) {
if (!suffix || foundSuffix) {
return controller.enqueue(chunk)
}
const content = decodeText(chunk, textDecoder)
if (content.endsWith(suffix)) {
foundSuffix = true
const contentWithoutSuffix = content.slice(0, -suffix.length)
controller.enqueue(encodeText(contentWithoutSuffix))
} else {
controller.enqueue(chunk)
}
},
flush(controller) { flush(controller) {
if (suffix) { if (suffix) {
controller.enqueue(encodeText(suffix)) controller.enqueue(encodeText(suffix))
...@@ -355,7 +373,6 @@ export async function continueFromInitialStream( ...@@ -355,7 +373,6 @@ export async function continueFromInitialStream(
serverInsertedHTMLToHead, serverInsertedHTMLToHead,
validateRootLayout, validateRootLayout,
}: { }: {
suffix?: string
dataStream?: ReadableStream<Uint8Array> dataStream?: ReadableStream<Uint8Array>
generateStaticHTML: boolean generateStaticHTML: boolean
getServerInsertedHTML?: () => Promise<string> getServerInsertedHTML?: () => Promise<string>
...@@ -364,9 +381,13 @@ export async function continueFromInitialStream( ...@@ -364,9 +381,13 @@ export async function continueFromInitialStream(
assetPrefix?: string assetPrefix?: string
getTree: () => FlightRouterState getTree: () => FlightRouterState
} }
// Suffix to inject after the buffered data, but before the close tags.
suffix?: string
} }
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>' 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)[0] : null const suffixUnclosed = suffix ? suffix.split(closeTag)[0] : null
if (generateStaticHTML) { if (generateStaticHTML) {
...@@ -374,13 +395,24 @@ export async function continueFromInitialStream( ...@@ -374,13 +395,24 @@ export async function continueFromInitialStream(
} }
const transforms: Array<TransformStream<Uint8Array, Uint8Array>> = [ const transforms: Array<TransformStream<Uint8Array, Uint8Array>> = [
// Buffer everything to avoid flushing too frequently
createBufferedTransformStream(), createBufferedTransformStream(),
// Insert generated tags to head
getServerInsertedHTML && !serverInsertedHTMLToHead getServerInsertedHTML && !serverInsertedHTMLToHead
? createInsertedHTMLStream(getServerInsertedHTML) ? createInsertedHTMLStream(getServerInsertedHTML)
: null, : null,
// Insert suffix content
suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null, suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null,
// Insert the flight data stream
dataStream ? createInlineDataStream(dataStream) : null, dataStream ? createInlineDataStream(dataStream) : null,
suffixUnclosed != null ? createSuffixStream(closeTag) : null,
// Close tags should always be deferred to the end
createSuffixStream(closeTag),
// Special head insertions
createHeadInsertionTransformStream(async () => { createHeadInsertionTransformStream(async () => {
// 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
// hydration errors. Remove this once it's ready to be handled by react itself. // hydration errors. Remove this once it's ready to be handled by react itself.
...@@ -390,6 +422,7 @@ export async function continueFromInitialStream( ...@@ -390,6 +422,7 @@ export async function continueFromInitialStream(
: '' : ''
return serverInsertedHTML return serverInsertedHTML
}), }),
validateRootLayout validateRootLayout
? createRootLayoutValidatorStream( ? createRootLayoutValidatorStream(
validateRootLayout.assetPrefix, validateRootLayout.assetPrefix,
......
...@@ -284,6 +284,15 @@ createNextDescribe( ...@@ -284,6 +284,15 @@ createNextDescribe(
expect(html).toContain('hello from app/dashboard') expect(html).toContain('hello from app/dashboard')
}) })
it('should ensure the </body></html> suffix is at the end of the stream', async () => {
const html = await next.render('/dashboard')
// It must end with the suffix and not contain it anywhere else.
const suffix = '</body></html>'
expect(html).toEndWith(suffix)
expect(html.slice(0, -suffix.length)).not.toContain(suffix)
})
if (!isNextDeploy) { if (!isNextDeploy) {
it('should serve /index as separate page', async () => { it('should serve /index as separate page', async () => {
const stderr = [] const stderr = []
......
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