Skip to content

Commit

Permalink
Update id env prefix (#51588)
Browse files Browse the repository at this point in the history
Removes `__` prefix and ensures it's available on client/server as
expected.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04TNLL0LSK/p1687197571572119)
  • Loading branch information
ijjk committed Jun 20, 2023
1 parent 2d2180f commit f6998e3
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export function getDefineEnv({
'process.env.__NEXT_ACTIONS_DEPLOYMENT_ID': JSON.stringify(
config.experimental.useDeploymentIdServerActions
),
'process.env.__NEXT_DEPLOYMENT_ID': JSON.stringify(
'process.env.NEXT_DEPLOYMENT_ID': JSON.stringify(
config.experimental.deploymentId
),
'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX':
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/client/app-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const addChunkSuffix =
return (
getOriginalChunk(chunkId) +
`${
process.env.__NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
process.env.NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}`
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ async function fetchServerAction(
'Next-Action': actionId,
[NEXT_ROUTER_STATE_TREE]: JSON.stringify(state.tree),
...(process.env.__NEXT_ACTIONS_DEPLOYMENT_ID &&
process.env.__NEXT_DEPLOYMENT_ID
process.env.NEXT_DEPLOYMENT_ID
? {
'x-deployment-id': process.env.__NEXT_DEPLOYMENT_ID,
'x-deployment-id': process.env.NEXT_DEPLOYMENT_ID,
}
: {}),
...(state.nextUrl
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const addChunkSuffix =
return (
getOriginalChunk(chunkId) +
`${
process.env.__NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
process.env.NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}`
)
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/client/route-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ function hasPrefetch(link?: HTMLLinkElement): boolean {
const canPrefetch: boolean = hasPrefetch()

const getAssetQueryString = () => {
return process.env.__NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
return process.env.NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default async function exportPage({

try {
if (renderOpts.deploymentId) {
process.env.__NEXT_DEPLOYMENT_ID = renderOpts.deploymentId
process.env.NEXT_DEPLOYMENT_ID = renderOpts.deploymentId
}
const { query: originalQuery = {} } = pathMap
const { page } = pathMap
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {

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
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ export default class NextNodeServer extends BaseServer {
this.compression = require('next/dist/compiled/compression')()
}

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

if (!this.minimalMode) {
this.imageResponseCache = new ResponseCache(this.minimalMode)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/shared/lib/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function defaultLoader({
return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${
quality || 75
}${
process.env.__NEXT_DEPLOYMENT_ID
? `&dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
process.env.NEXT_DEPLOYMENT_ID
? `&dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

export function GET(req) {
return NextResponse.json({
deploymentId: process.env.NEXT_DEPLOYMENT_ID,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function Page() {
<>
<p>hello app edge</p>
<Image src={testImage} alt="test" />
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>

<button
onClick={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function Page() {
<>
<p>hello app</p>
<Image src={testImage} alt="test" />
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>

<button
onClick={() => {
Expand Down
3 changes: 3 additions & 0 deletions test/production/deployment-id-handling/app/pages/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function handler(_req, res) {
res.json({ deploymentId: process.env.NEXT_DEPLOYMENT_ID })
}
1 change: 1 addition & 0 deletions test/production/deployment-id-handling/app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function Page() {
return (
<>
<p className={styles.template}>hello pages</p>
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>
<Image src={testImage} alt="test image" />

<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function Page() {
<>
<p>hello pages edge</p>
<Image src={testImage} alt="test image" />
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>

<button
onClick={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ createNextDescribe(
async ({ urlPath }) => {
const $ = await next.render$(urlPath)

expect($('#deploymentId').text()).toBe(deploymentId)

const scripts = Array.from($('script'))
expect(scripts.length).toBeGreaterThan(0)

Expand Down Expand Up @@ -64,5 +66,16 @@ createNextDescribe(
}
}
)

it.each([{ pathname: '/api/hello' }, { pathname: '/api/hello-app' }])(
'should have deployment id env available',
async ({ pathname }) => {
const res = await next.fetch(pathname)

expect(await res.json()).toEqual({
deploymentId,
})
}
)
}
)

0 comments on commit f6998e3

Please sign in to comment.