Unverified Commit 691613f8 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix deploy id env handling when disabled (#57374)

Ensure we properly omit the `dpl` fields when the experimental config
isn't enabled but the env is present.
parent 23f32465
...@@ -374,6 +374,8 @@ export default async function build( ...@@ -374,6 +374,8 @@ export default async function build(
silent: false, silent: false,
}) })
) )
process.env.NEXT_DEPLOYMENT_ID = config.experimental.deploymentId || ''
NextBuildContext.config = config NextBuildContext.config = config
let configOutDir = 'out' let configOutDir = 'out'
......
...@@ -99,7 +99,7 @@ export function getDefineEnv({ ...@@ -99,7 +99,7 @@ export function getDefineEnv({
config.experimental.useDeploymentIdServerActions config.experimental.useDeploymentIdServerActions
), ),
'process.env.NEXT_DEPLOYMENT_ID': JSON.stringify( 'process.env.NEXT_DEPLOYMENT_ID': JSON.stringify(
config.experimental.deploymentId config.experimental.deploymentId || false
), ),
'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX': 'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX':
JSON.stringify(fetchCacheKeyPrefix), JSON.stringify(fetchCacheKeyPrefix),
......
...@@ -64,9 +64,6 @@ async function exportPageImpl( ...@@ -64,9 +64,6 @@ async function exportPageImpl(
trailingSlash, trailingSlash,
} = input } = input
if (input.renderOpts.deploymentId) {
process.env.NEXT_DEPLOYMENT_ID = input.renderOpts.deploymentId
}
if (enableExperimentalReact) { if (enableExperimentalReact) {
process.env.__NEXT_EXPERIMENTAL_REACT = 'true' process.env.__NEXT_EXPERIMENTAL_REACT = 'true'
} }
......
...@@ -471,10 +471,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -471,10 +471,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.nextFontManifest = this.getNextFontManifest() this.nextFontManifest = this.getNextFontManifest()
if (process.env.NEXT_RUNTIME !== 'edge') { if (process.env.NEXT_RUNTIME !== 'edge') {
if (this.nextConfig.experimental.deploymentId) {
process.env.NEXT_DEPLOYMENT_ID = process.env.NEXT_DEPLOYMENT_ID =
this.nextConfig.experimental.deploymentId this.nextConfig.experimental.deploymentId || ''
}
} }
this.renderOpts = { this.renderOpts = {
......
...@@ -518,6 +518,11 @@ function assignDefaults( ...@@ -518,6 +518,11 @@ function assignDefaults(
result.experimental.deploymentId = process.env.NEXT_DEPLOYMENT_ID result.experimental.deploymentId = process.env.NEXT_DEPLOYMENT_ID
} }
// can't use this one without the other
if (result.experimental?.useDeploymentIdServerActions) {
result.experimental.useDeploymentId = true
}
// use the closest lockfile as tracing root // use the closest lockfile as tracing root
if (!result.experimental?.outputFileTracingRoot) { if (!result.experimental?.outputFileTracingRoot) {
let rootDir = findRootDir(dir) let rootDir = findRootDir(dir)
......
...@@ -180,10 +180,8 @@ export default class NextNodeServer extends BaseServer { ...@@ -180,10 +180,8 @@ export default class NextNodeServer extends BaseServer {
if (this.renderOpts.nextScriptWorkers) { if (this.renderOpts.nextScriptWorkers) {
process.env.__NEXT_SCRIPT_WORKERS = JSON.stringify(true) process.env.__NEXT_SCRIPT_WORKERS = JSON.stringify(true)
} }
process.env.NEXT_DEPLOYMENT_ID =
if (this.nextConfig.experimental.deploymentId) { this.nextConfig.experimental.deploymentId || ''
process.env.NEXT_DEPLOYMENT_ID = this.nextConfig.experimental.deploymentId
}
if (!this.minimalMode) { if (!this.minimalMode) {
this.imageResponseCache = new ResponseCache(this.minimalMode) this.imageResponseCache = new ResponseCache(this.minimalMode)
......
module.exports = { module.exports = {
experimental: { experimental: {
useDeploymentId: true, useDeploymentId: !!process.env.USE_DEPLOYMENT_ID,
}, },
} }
...@@ -5,11 +5,12 @@ import { join } from 'node:path' ...@@ -5,11 +5,12 @@ import { join } from 'node:path'
const deploymentId = Date.now() + '' const deploymentId = Date.now() + ''
createNextDescribe( createNextDescribe(
'deployment-id-handling', 'deployment-id-handling enabled',
{ {
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
env: { env: {
NEXT_DEPLOYMENT_ID: deploymentId, NEXT_DEPLOYMENT_ID: deploymentId,
USE_DEPLOYMENT_ID: '1',
}, },
}, },
({ next }) => { ({ next }) => {
...@@ -83,3 +84,83 @@ createNextDescribe( ...@@ -83,3 +84,83 @@ createNextDescribe(
) )
} }
) )
createNextDescribe(
'deployment-id-handling disabled',
{
files: join(__dirname, 'app'),
env: {
NEXT_DEPLOYMENT_ID: deploymentId,
},
},
({ next }) => {
it.each([
{ urlPath: '/' },
{ urlPath: '/pages-edge' },
{ urlPath: '/from-app' },
{ urlPath: '/from-app/edge' },
])(
'should not append dpl query to all assets for $urlPath',
async ({ urlPath }) => {
const $ = await next.render$(urlPath)
expect($('#deploymentId').text()).not.toBe(deploymentId)
const scripts = Array.from($('script'))
expect(scripts.length).toBeGreaterThan(0)
for (const script of scripts) {
if (script.attribs.src) {
expect(script.attribs.src).not.toContain('dpl=' + deploymentId)
}
}
const links = Array.from($('link'))
expect(links.length).toBeGreaterThan(0)
for (const link of links) {
if (link.attribs.href) {
if (link.attribs.as === 'font') {
expect(link.attribs.href).not.toContain('dpl=' + deploymentId)
} else {
expect(link.attribs.href).not.toContain('dpl=' + deploymentId)
}
}
}
const browser = await next.browser(urlPath)
const requests = []
browser.on('request', (req) => {
requests.push(req.url())
})
await browser.elementByCss('#dynamic-import').click()
await check(
() => (requests.length > 0 ? 'success' : JSON.stringify(requests)),
'success'
)
try {
expect(
requests.every((item) => !item.includes('dpl=' + deploymentId))
).toBe(true)
} finally {
require('console').error('requests', requests)
}
}
)
it.each([{ pathname: '/api/hello' }, { pathname: '/api/hello-app' }])(
'should not have deployment id env available',
async ({ pathname }) => {
const res = await next.fetch(pathname)
expect(await res.json()).not.toEqual({
deploymentId,
})
}
)
}
)
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