Unverified Commit 03a1815a authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Inject `<html data-dpl-id>` and don't inline it into JS anymore (#88761)

We don't inline `process.env.NEXT_DEPLOYMENT_ID` anymore into browser chunks, and instead read it from `<html data-dpl-id` attribute at runtime.

Closes PACK-6538

- [x] ~~Causes a hydration error at the moment.~~ Solved by removing the attribute immediately before hydration happens
<img width="1443" height="620" alt="Bildschirmfoto 2026-01-20 um 12 32 06" src="https://github.com/user-attachments/assets/b69b3a0b-129d-44f4-84d1-10572b650437" />
parent eb14325e
......@@ -177,12 +177,10 @@ export function getDefineEnv({
: isClient
? isTurbopack
? {
// This is set at runtime by packages/next/src/client/register-deployment-id-global.ts
'process.env.NEXT_DEPLOYMENT_ID': {
[DEFINE_ENV_EXPRESSION]: 'globalThis.NEXT_DEPLOYMENT_ID',
},
// TODO replace with read from HTML document attribute
'process.env.NEXT_DEPLOYMENT_ID_COMPILE_TIME':
config.deploymentId,
}
: {
// For Webpack, we currently don't use the non-inlining globalThis.NEXT_DEPLOYMENT_ID
......
......@@ -928,13 +928,24 @@ export function Html(
HTMLHtmlElement
>
) {
const { docComponentsRendered, locale, scriptLoader, __NEXT_DATA__ } =
useHtmlContext()
const {
docComponentsRendered,
locale,
scriptLoader,
deploymentId,
__NEXT_DATA__,
} = useHtmlContext()
docComponentsRendered.Html = true
handleDocumentScriptLoaderItems(scriptLoader, __NEXT_DATA__, props)
return <html {...props} lang={props.lang || locale || undefined} />
return (
<html
{...props}
lang={props.lang || locale || undefined}
data-dpl-id={deploymentId || undefined}
/>
)
}
export function Main() {
......
......@@ -2884,6 +2884,7 @@ async function renderToStream(
),
getServerInsertedHTML,
getServerInsertedMetadata,
deploymentId: ctx.renderOpts.deploymentId,
})
}
}
......@@ -2961,6 +2962,7 @@ async function renderToStream(
isStaticGeneration: generateStaticHTML,
isBuildTimePrerendering: ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.renderOpts.deploymentId,
getServerInsertedHTML,
getServerInsertedMetadata,
validateRootLayout: dev,
......@@ -3130,6 +3132,7 @@ async function renderToStream(
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.renderOpts.deploymentId,
getServerInsertedHTML: makeGetServerInsertedHTML({
polyfills,
renderServerInsertedHTML,
......@@ -4839,6 +4842,7 @@ async function prerenderToStream(
stream: await continueDynamicPrerender(prelude, {
getServerInsertedHTML,
getServerInsertedMetadata,
deploymentId: ctx.renderOpts.deploymentId,
}),
dynamicAccess: consumeDynamicAccess(
serverDynamicTracking,
......@@ -4934,6 +4938,7 @@ async function prerenderToStream(
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.renderOpts.deploymentId,
})
} else {
// Normal static prerender case, no fallback param handling needed
......@@ -4948,6 +4953,7 @@ async function prerenderToStream(
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.renderOpts.deploymentId,
})
}
......@@ -5119,6 +5125,7 @@ async function prerenderToStream(
stream: await continueDynamicPrerender(prelude, {
getServerInsertedHTML,
getServerInsertedMetadata,
deploymentId: ctx.renderOpts.deploymentId,
}),
dynamicAccess: dynamicTracking.dynamicAccesses,
// TODO: Should this include the SSR pass?
......@@ -5140,6 +5147,7 @@ async function prerenderToStream(
stream: await continueDynamicPrerender(prelude, {
getServerInsertedHTML,
getServerInsertedMetadata,
deploymentId: ctx.renderOpts.deploymentId,
}),
dynamicAccess: dynamicTracking.dynamicAccesses,
// TODO: Should this include the SSR pass?
......@@ -5206,6 +5214,7 @@ async function prerenderToStream(
isBuildTimePrerendering:
ctx.workStore.isBuildTimePrerendering === true,
buildId: ctx.workStore.buildId,
deploymentId: ctx.renderOpts.deploymentId,
}),
dynamicAccess: dynamicTracking.dynamicAccesses,
// TODO: Should this include the SSR pass?
......@@ -5306,6 +5315,7 @@ async function prerenderToStream(
buildId: ctx.workStore.buildId,
getServerInsertedHTML,
getServerInsertedMetadata,
deploymentId: ctx.renderOpts.deploymentId,
}),
// TODO: Should this include the SSR pass?
collectedRevalidate: prerenderLegacyStore.revalidate,
......@@ -5491,6 +5501,7 @@ async function prerenderToStream(
}),
getServerInsertedMetadata,
validateRootLayout: dev,
deploymentId: ctx.renderOpts.deploymentId,
}),
dynamicAccess: null,
collectedRevalidate:
......
......@@ -1499,6 +1499,7 @@ export async function renderToHTMLImpl(
docComponentsRendered,
dangerousAsPath: router.asPath,
isDevelopment: !!dev,
deploymentId: sharedContext.deploymentId,
dynamicImports: Array.from(dynamicImports),
dynamicCssManifest: new Set(renderOpts.dynamicCssManifest || []),
assetPrefix,
......
// This could also be a variable instead of a function, but some unit tests want to change the ID at
// runtime. Even though that would never happen in a real deployment.
let deploymentId: string | undefined
if (typeof window !== 'undefined') {
deploymentId = document.documentElement.dataset.dplId
// Immediately remove the attribute to prevent hydration errors (the dplId was inserted into the
// HTML only), React isn't aware of it at all.
delete document.documentElement.dataset.dplId
} else {
// Client side: replaced with globalThis.NEXT_DEPLOYMENT_ID
// Server side: left as is or replaced with a string or replaced with false
deploymentId = process.env.NEXT_DEPLOYMENT_ID || undefined
}
export function getDeploymentId(): string | undefined {
return (
process.env.NEXT_DEPLOYMENT_ID ??
process.env.NEXT_DEPLOYMENT_ID_COMPILE_TIME
)
return deploymentId
}
export function getDeploymentIdQueryOrEmptyString(): string {
let deploymentId = getDeploymentId()
if (deploymentId) {
return `?dpl=${deploymentId}`
}
......
......@@ -18,6 +18,7 @@ export type HtmlProps = {
}
buildManifest: BuildManifest
isDevelopment: boolean
deploymentId: string | undefined
dynamicImports: string[]
/**
* This manifest is only needed for Pages dir, Production, Webpack
......
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