Unverified Commit f91113d7 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Don't insert build id comment in HTML (#89478)

The build id comment in HTML documents was added in  https://github.com/vercel/next.js/pull/81247
But it was removed again to detect redirects using a HEAD request instead in https://github.com/vercel/next.js/pull/85910
Meaning the build id comment is completely unused now, so no need to insert it

see https://vercel.slack.com/archives/C04KC8A53T7/p1770196901369359

Closes PACK-6846
parent ca1e1c7d
...@@ -2998,8 +2998,6 @@ async function renderToStream( ...@@ -2998,8 +2998,6 @@ async function renderToStream(
formState formState
), ),
isStaticGeneration: generateStaticHTML, isStaticGeneration: generateStaticHTML,
isBuildTimePrerendering: ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
...@@ -3167,9 +3165,6 @@ async function renderToStream( ...@@ -3167,9 +3165,6 @@ async function renderToStream(
formState formState
), ),
isStaticGeneration: generateStaticHTML, isStaticGeneration: generateStaticHTML,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
getServerInsertedHTML: makeGetServerInsertedHTML({ getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills, polyfills,
...@@ -5000,9 +4995,6 @@ async function prerenderToStream( ...@@ -5000,9 +4995,6 @@ async function prerenderToStream(
), ),
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
}) })
} else { } else {
...@@ -5015,9 +5007,6 @@ async function prerenderToStream( ...@@ -5015,9 +5007,6 @@ async function prerenderToStream(
), ),
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
}) })
} }
...@@ -5278,9 +5267,6 @@ async function prerenderToStream( ...@@ -5278,9 +5267,6 @@ async function prerenderToStream(
), ),
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
}), }),
dynamicAccess: dynamicTracking.dynamicAccesses, dynamicAccess: dynamicTracking.dynamicAccesses,
...@@ -5377,9 +5363,6 @@ async function prerenderToStream( ...@@ -5377,9 +5363,6 @@ async function prerenderToStream(
formState formState
), ),
isStaticGeneration: true, isStaticGeneration: true,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
deploymentId: ctx.sharedContext.deploymentId, deploymentId: ctx.sharedContext.deploymentId,
...@@ -5556,9 +5539,6 @@ async function prerenderToStream( ...@@ -5556,9 +5539,6 @@ async function prerenderToStream(
formState formState
), ),
isStaticGeneration: true, isStaticGeneration: true,
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
getServerInsertedHTML: makeGetServerInsertedHTML({ getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills, polyfills,
renderServerInsertedHTML, renderServerInsertedHTML,
......
...@@ -14,7 +14,6 @@ import { ...@@ -14,7 +14,6 @@ import {
removeFromUint8Array, removeFromUint8Array,
} from './uint8array-helpers' } from './uint8array-helpers'
import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants' import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants'
import { insertBuildIdComment } from '../../shared/lib/segment-cache/output-export-prefetch-encoding'
import { import {
RSC_HEADER, RSC_HEADER,
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
...@@ -235,32 +234,35 @@ export function createBufferedTransformStream( ...@@ -235,32 +234,35 @@ export function createBufferedTransformStream(
}) })
} }
function createPrefetchCommentStream( // TODO this is currently unused but once we add proper output:export support, it needs to be
isBuildTimePrerendering: boolean, // revisited. See https://github.com/vercel/next.js/pull/89478 for more details
buildId: string //
): TransformStream<Uint8Array, Uint8Array> { // function createPrefetchCommentStream(
// Insert an extra comment at the beginning of the HTML document. This must // isBuildTimePrerendering: boolean,
// come after the DOCTYPE, which is inserted by React. // buildId: string
// // ): TransformStream<Uint8Array, Uint8Array> {
// The first chunk sent by React will contain the doctype. After that, we can // // Insert an extra comment at the beginning of the HTML document. This must
// pass through the rest of the chunks as-is. // // come after the DOCTYPE, which is inserted by React.
let didTransformFirstChunk = false // //
return new TransformStream({ // // The first chunk sent by React will contain the doctype. After that, we can
transform(chunk, controller) { // // pass through the rest of the chunks as-is.
if (isBuildTimePrerendering && !didTransformFirstChunk) { // let didTransformFirstChunk = false
didTransformFirstChunk = true // return new TransformStream({
const decoder = new TextDecoder('utf-8', { fatal: true }) // transform(chunk, controller) {
const chunkStr = decoder.decode(chunk, { // if (isBuildTimePrerendering && !didTransformFirstChunk) {
stream: true, // didTransformFirstChunk = true
}) // const decoder = new TextDecoder('utf-8', { fatal: true })
const updatedChunkStr = insertBuildIdComment(chunkStr, buildId) // const chunkStr = decoder.decode(chunk, {
controller.enqueue(encoder.encode(updatedChunkStr)) // stream: true,
return // })
} // const updatedChunkStr = insertBuildIdComment(chunkStr, buildId)
controller.enqueue(chunk) // controller.enqueue(encoder.encode(updatedChunkStr))
}, // return
}) // }
} // controller.enqueue(chunk)
// },
// })
// }
export function renderToInitialFizzStream({ export function renderToInitialFizzStream({
ReactDOMServer, ReactDOMServer,
...@@ -839,8 +841,6 @@ function chainTransformers<T>( ...@@ -839,8 +841,6 @@ function chainTransformers<T>(
export type ContinueStreamOptions = { export type ContinueStreamOptions = {
inlinedDataStream: ReadableStream<Uint8Array> | undefined inlinedDataStream: ReadableStream<Uint8Array> | undefined
isStaticGeneration: boolean isStaticGeneration: boolean
isBuildTimePrerendering: boolean
buildId: string
deploymentId: string | undefined deploymentId: string | undefined
getServerInsertedHTML: () => Promise<string> getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string> getServerInsertedMetadata: () => Promise<string>
...@@ -857,8 +857,6 @@ export async function continueFizzStream( ...@@ -857,8 +857,6 @@ export async function continueFizzStream(
suffix, suffix,
inlinedDataStream, inlinedDataStream,
isStaticGeneration, isStaticGeneration,
isBuildTimePrerendering,
buildId,
deploymentId, deploymentId,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
...@@ -881,9 +879,6 @@ export async function continueFizzStream( ...@@ -881,9 +879,6 @@ export async function continueFizzStream(
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
createBufferedTransformStream(), createBufferedTransformStream(),
// Add build id comment to start of the HTML document (in export mode)
createPrefetchCommentStream(isBuildTimePrerendering, buildId),
// Insert data-dpl-id attribute on the html tag // Insert data-dpl-id attribute on the html tag
deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null, deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null,
...@@ -944,8 +939,6 @@ type ContinueStaticPrerenderOptions = { ...@@ -944,8 +939,6 @@ type ContinueStaticPrerenderOptions = {
inlinedDataStream: ReadableStream<Uint8Array> inlinedDataStream: ReadableStream<Uint8Array>
getServerInsertedHTML: () => Promise<string> getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string> getServerInsertedMetadata: () => Promise<string>
isBuildTimePrerendering: boolean
buildId: string
deploymentId: string | undefined deploymentId: string | undefined
} }
...@@ -955,8 +948,6 @@ export async function continueStaticPrerender( ...@@ -955,8 +948,6 @@ export async function continueStaticPrerender(
inlinedDataStream, inlinedDataStream,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
isBuildTimePrerendering,
buildId,
deploymentId, deploymentId,
}: ContinueStaticPrerenderOptions }: ContinueStaticPrerenderOptions
) { ) {
...@@ -964,7 +955,6 @@ export async function continueStaticPrerender( ...@@ -964,7 +955,6 @@ export async function continueStaticPrerender(
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
createBufferedTransformStream(), createBufferedTransformStream(),
// Add build id comment to start of the HTML document (in export mode) // Add build id comment to start of the HTML document (in export mode)
createPrefetchCommentStream(isBuildTimePrerendering, buildId),
// Insert data-dpl-id attribute on the html tag // Insert data-dpl-id attribute on the html tag
deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null, deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null,
// Insert generated tags to head // Insert generated tags to head
...@@ -984,8 +974,6 @@ export async function continueStaticFallbackPrerender( ...@@ -984,8 +974,6 @@ export async function continueStaticFallbackPrerender(
inlinedDataStream, inlinedDataStream,
getServerInsertedHTML, getServerInsertedHTML,
getServerInsertedMetadata, getServerInsertedMetadata,
isBuildTimePrerendering,
buildId,
deploymentId, deploymentId,
}: ContinueStaticPrerenderOptions }: ContinueStaticPrerenderOptions
) { ) {
...@@ -995,8 +983,6 @@ export async function continueStaticFallbackPrerender( ...@@ -995,8 +983,6 @@ export async function continueStaticFallbackPrerender(
return chainTransformers(prerenderStream, [ return chainTransformers(prerenderStream, [
// Buffer everything to avoid flushing too frequently // Buffer everything to avoid flushing too frequently
createBufferedTransformStream(), createBufferedTransformStream(),
// Add build id comment to start of the HTML document (in export mode)
createPrefetchCommentStream(isBuildTimePrerendering, buildId),
// Insert data-dpl-id attribute on the html tag // Insert data-dpl-id attribute on the html tag
deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null, deploymentId ? createHtmlDataDplIdTransformStream(deploymentId) : null,
// Insert generated tags to head // Insert generated tags to head
......
// In output: export mode, the build id is added to the start of the HTML
// document, directly after the doctype declaration. During a prefetch, the
// client performs a range request to get the build id, so it can check whether
// the target page belongs to the same build.
//
// The first 64 bytes of the document are requested. The exact number isn't
// too important; it must be larger than the build id + doctype + closing and
// ending comment markers, but it doesn't need to match the end of the
// comment exactly.
//
// Build ids are 21 bytes long in the default implementation, though this
// can be overridden in the Next.js config. For the purposes of this check,
// it's OK to only match the start of the id, so we'll truncate it if exceeds
// a certain length.
const DOCTYPE_PREFIX = '<!DOCTYPE html>' // 15 bytes
const MAX_BUILD_ID_LENGTH = 24
function escapeBuildId(buildId: string) {
// If the build id is longer than the given limit, it's OK for our purposes
// to only match the beginning.
const truncated = buildId.slice(0, MAX_BUILD_ID_LENGTH)
// Replace hyphens with underscores so it doesn't break the HTML comment.
// (Unlikely, but if this did happen it would break the whole document.)
return truncated.replace(/-/g, '_')
}
export function insertBuildIdComment(originalHtml: string, buildId: string) {
if (
// Skip if the build id contains a closing comment marker.
buildId.includes('-->') ||
// React always inserts a doctype at the start of the document. Skip if it
// isn't present. Shouldn't happen; suggests an issue elsewhere.
!originalHtml.startsWith(DOCTYPE_PREFIX)
) {
// Return the original HTML unchanged. This means the document will not
// be prefetched.
// TODO: The build id comment is currently only used during prefetches, but
// if we eventually use this mechanism for regular navigations, we may need
// to error during build if we fail to insert it for some reason.
return originalHtml
}
// The comment must be inserted after the doctype.
return originalHtml.replace(
DOCTYPE_PREFIX,
DOCTYPE_PREFIX + '<!--' + escapeBuildId(buildId) + '-->'
)
}
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