Skip to content

Commit 7cdb4d0

Browse files
authored
Abstract deployment id access into module (#86727)
This also helps with persistent-caching performance, since fewer chunks change (only the ones containing this module, as opposed to the chunks containing all the users of the value)
1 parent 342adb0 commit 7cdb4d0

File tree

13 files changed

+42
-27
lines changed

13 files changed

+42
-27
lines changed

packages/next/src/build/deployment-id.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/next/src/build/webpack/plugins/flight-manifest-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getProxiedPluginState } from '../../build-context'
1919
import { WEBPACK_LAYERS } from '../../../lib/constants'
2020
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
2121
import { CLIENT_STATIC_FILES_RUNTIME_MAIN_APP } from '../../../shared/lib/constants'
22-
import { getDeploymentIdQueryOrEmptyString } from '../../deployment-id'
22+
import { getDeploymentIdQueryOrEmptyString } from '../../../shared/lib/deployment-id'
2323
import {
2424
formatBarrelOptimizedResource,
2525
getModuleReferencesInOrder,

packages/next/src/client/app-webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Override chunk URL mapping in the webpack runtime
22
// https://github.com/webpack/webpack/blob/2738eebc7880835d88c727d364ad37f3ec557593/lib/RuntimeGlobals.js#L204
33

4-
import { getDeploymentIdQueryOrEmptyString } from '../build/deployment-id'
4+
import { getDeploymentIdQueryOrEmptyString } from '../shared/lib/deployment-id'
55
import { encodeURIPath } from '../shared/lib/encode-uri-path'
66

77
declare const __webpack_require__: any

packages/next/src/client/components/app-router.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import RootErrorBoundary from './errors/root-error-boundary'
4646
import DefaultGlobalError from './builtin/global-error'
4747
import { RootLayoutBoundary } from '../../lib/framework/boundary-components'
4848
import type { StaticIndicatorState } from '../dev/hot-reloader/app/hot-reloader-app'
49+
import { getDeploymentIdQueryOrEmptyString } from '../../shared/lib/deployment-id'
4950

5051
const globalMutable: {
5152
pendingMpaPath?: string
@@ -622,9 +623,7 @@ function RuntimeStyles() {
622623
}
623624
}, [renderedStylesSize, forceUpdate])
624625

625-
const dplId = process.env.NEXT_DEPLOYMENT_ID
626-
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
627-
: ''
626+
const dplId = getDeploymentIdQueryOrEmptyString()
628627
return [...runtimeStyles].map((href, i) => (
629628
<link
630629
key={i}

packages/next/src/client/components/router-reducer/fetch-server-response.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
urlToUrlWithoutFlightMarker,
4242
} from '../../route-params'
4343
import type { NormalizedSearch } from '../segment-cache/cache-key'
44+
import { getDeploymentId } from '../../../shared/lib/deployment-id'
4445

4546
const createFromReadableStream =
4647
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
@@ -329,8 +330,9 @@ export async function createFetch<T>(
329330
headers['Next-Test-Fetch-Priority'] = fetchPriority
330331
}
331332

332-
if (process.env.NEXT_DEPLOYMENT_ID) {
333-
headers['x-deployment-id'] = process.env.NEXT_DEPLOYMENT_ID
333+
const deploymentId = getDeploymentId()
334+
if (deploymentId) {
335+
headers['x-deployment-id'] = deploymentId
334336
}
335337

336338
if (process.env.NODE_ENV !== 'production') {

packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
omitUnusedArgs,
5757
} from '../../../../shared/lib/server-reference-info'
5858
import { revalidateEntireCache } from '../../segment-cache/cache'
59+
import { getDeploymentId } from '../../../../shared/lib/deployment-id'
5960

6061
const createFromFetch =
6162
createFromFetchBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromFetch']
@@ -110,8 +111,9 @@ async function fetchServerAction(
110111
),
111112
}
112113

113-
if (process.env.NEXT_DEPLOYMENT_ID) {
114-
headers['x-deployment-id'] = process.env.NEXT_DEPLOYMENT_ID
114+
const deploymentId = getDeploymentId()
115+
if (deploymentId) {
116+
headers['x-deployment-id'] = deploymentId
115117
}
116118

117119
if (nextUrl) {

packages/next/src/client/route-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ProxyMatcher } from '../build/analysis/get-page-static-info'
33
import getAssetPathFromRoute from '../shared/lib/router/utils/get-asset-path-from-route'
44
import { __unsafeCreateTrustedScriptURL } from './trusted-types'
55
import { requestIdleCallback } from './request-idle-callback'
6-
import { getDeploymentIdQueryOrEmptyString } from '../build/deployment-id'
6+
import { getDeploymentIdQueryOrEmptyString } from '../shared/lib/deployment-id'
77
import { encodeURIPath } from '../shared/lib/encode-uri-path'
88

99
// 3.8s was arbitrarily chosen as it's what https://web.dev/interactive

packages/next/src/client/webpack.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
declare const __webpack_require__: any
22
declare let __webpack_public_path__: string
33

4-
import { getDeploymentIdQueryOrEmptyString } from '../build/deployment-id'
4+
import {
5+
getDeploymentId,
6+
getDeploymentIdQueryOrEmptyString,
7+
} from '../shared/lib/deployment-id'
58

69
// If we have a deployment ID, we need to append it to the webpack chunk names
710
// I am keeping the process check explicit so this can be statically optimized
8-
if (process.env.NEXT_DEPLOYMENT_ID) {
11+
if (getDeploymentId()) {
912
const suffix = getDeploymentIdQueryOrEmptyString()
1013
const getChunkScriptFilename = __webpack_require__.u
1114
__webpack_require__.u = (...args: any[]) =>

packages/next/src/server/route-modules/pages/pages-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
GetStaticPaths,
4343
GetStaticProps,
4444
} from '../../../types'
45+
import { getDeploymentId } from '../../../shared/lib/deployment-id'
4546

4647
export const getHandler = ({
4748
srcPage: originalSrcPage,
@@ -257,7 +258,7 @@ export const getHandler = ({
257258
buildId,
258259
customServer:
259260
Boolean(routerServerContext?.isCustomServer) || undefined,
260-
deploymentId: process.env.NEXT_DEPLOYMENT_ID,
261+
deploymentId: getDeploymentId(),
261262
},
262263
renderOpts: {
263264
params,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This could also be a variable instead of a function, but some unit tests want to change the ID at
2+
// runtime. Even though that would never happen in a real deployment.
3+
export function getDeploymentId(): string | undefined {
4+
return process.env.NEXT_DEPLOYMENT_ID
5+
}
6+
7+
export function getDeploymentIdQueryOrEmptyString(): string {
8+
let deploymentId = getDeploymentId()
9+
if (deploymentId) {
10+
return `?dpl=${deploymentId}`
11+
}
12+
return ''
13+
}

0 commit comments

Comments
 (0)