Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deploy id env handling when disabled #57374

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ export default async function build(
silent: false,
})
)

process.env.NEXT_DEPLOYMENT_ID = config.experimental.deploymentId || ''
NextBuildContext.config = config

let configOutDir = 'out'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function getDefineEnv({
config.experimental.useDeploymentIdServerActions
),
'process.env.NEXT_DEPLOYMENT_ID': JSON.stringify(
config.experimental.deploymentId
config.experimental.deploymentId || false
),
'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX':
JSON.stringify(fetchCacheKeyPrefix),
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ async function exportPageImpl(
trailingSlash,
} = input

if (input.renderOpts.deploymentId) {
process.env.NEXT_DEPLOYMENT_ID = input.renderOpts.deploymentId
}
if (enableExperimentalReact) {
process.env.__NEXT_EXPERIMENTAL_REACT = 'true'
}
Expand Down
6 changes: 2 additions & 4 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.nextFontManifest = this.getNextFontManifest()

if (process.env.NEXT_RUNTIME !== 'edge') {
if (this.nextConfig.experimental.deploymentId) {
process.env.NEXT_DEPLOYMENT_ID =
this.nextConfig.experimental.deploymentId
}
process.env.NEXT_DEPLOYMENT_ID =
this.nextConfig.experimental.deploymentId || ''
}

this.renderOpts = {
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ function assignDefaults(
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
if (!result.experimental?.outputFileTracingRoot) {
let rootDir = findRootDir(dir)
Expand Down
6 changes: 2 additions & 4 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ export default class NextNodeServer extends BaseServer {
if (this.renderOpts.nextScriptWorkers) {
process.env.__NEXT_SCRIPT_WORKERS = JSON.stringify(true)
}

if (this.nextConfig.experimental.deploymentId) {
process.env.NEXT_DEPLOYMENT_ID = this.nextConfig.experimental.deploymentId
}
process.env.NEXT_DEPLOYMENT_ID =
this.nextConfig.experimental.deploymentId || ''

if (!this.minimalMode) {
this.imageResponseCache = new ResponseCache(this.minimalMode)
Expand Down
2 changes: 1 addition & 1 deletion test/production/deployment-id-handling/app/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
experimental: {
useDeploymentId: true,
useDeploymentId: !!process.env.USE_DEPLOYMENT_ID,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { join } from 'node:path'
const deploymentId = Date.now() + ''

createNextDescribe(
'deployment-id-handling',
'deployment-id-handling enabled',
{
files: join(__dirname, 'app'),
env: {
NEXT_DEPLOYMENT_ID: deploymentId,
USE_DEPLOYMENT_ID: '1',
},
},
({ next }) => {
Expand Down Expand Up @@ -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,
})
}
)
}
)
Loading