Skip to content

Commit

Permalink
Add experimental id handling (#50470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored and wyattjoh committed Jun 1, 2023
1 parent 46d1287 commit 04bbb69
Show file tree
Hide file tree
Showing 25 changed files with 276 additions and 63 deletions.
6 changes: 6 additions & 0 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export function getDefineEnv({
'process.env.NEXT_RUNTIME': JSON.stringify(
isEdgeServer ? 'edge' : isNodeServer ? 'nodejs' : undefined
),
'process.env.__NEXT_ACTIONS_DEPLOYMENT_ID': JSON.stringify(
config.experimental.useDeploymentIdServerActions
),
'process.env.__NEXT_DEPLOYMENT_ID': JSON.stringify(
config.experimental.deploymentId
),
'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX':
JSON.stringify(fetchCacheKeyPrefix),
'process.env.__NEXT_PREVIEW_MODE_ID': JSON.stringify(previewModeId),
Expand Down
9 changes: 8 additions & 1 deletion packages/next/src/client/app-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ const chunkFilenameMap: any = {}

// eslint-disable-next-line no-undef
__webpack_require__.u = (chunkId: any) => {
return encodeURI(chunkFilenameMap[chunkId] || getChunkScriptFilename(chunkId))
return (
encodeURI(chunkFilenameMap[chunkId] || getChunkScriptFilename(chunkId)) +
`${
process.env.__NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
: ''
}`
)
}

// Ignore the module ID transform in client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ async function fetchServerAction(
Accept: RSC_CONTENT_TYPE_HEADER,
'Next-Action': actionId,
[NEXT_ROUTER_STATE_TREE]: JSON.stringify(state.tree),
...(process.env.__NEXT_ACTIONS_DEPLOYMENT_ID &&
process.env.__NEXT_DEPLOYMENT_ID
? {
'x-deployment-id': process.env.__NEXT_DEPLOYMENT_ID,
}
: {}),
...(state.nextUrl
? {
[NEXT_URL]: state.nextUrl,
Expand Down
15 changes: 12 additions & 3 deletions packages/next/src/client/route-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ function hasPrefetch(link?: HTMLLinkElement): boolean {

const canPrefetch: boolean = hasPrefetch()

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

function prefetchViaDom(
href: string,
as: string,
Expand Down Expand Up @@ -246,7 +252,8 @@ function getFilesForRoute(
const scriptUrl =
assetPrefix +
'/_next/static/chunks/pages' +
encodeURI(getAssetPathFromRoute(route, '.js'))
encodeURI(getAssetPathFromRoute(route, '.js')) +
getAssetQueryString()
return Promise.resolve({
scripts: [__unsafeCreateTrustedScriptURL(scriptUrl)],
// Styles are handled by `style-loader` in development:
Expand All @@ -263,8 +270,10 @@ function getFilesForRoute(
return {
scripts: allFiles
.filter((v) => v.endsWith('.js'))
.map((v) => __unsafeCreateTrustedScriptURL(v)),
css: allFiles.filter((v) => v.endsWith('.css')),
.map((v) => __unsafeCreateTrustedScriptURL(v) + getAssetQueryString()),
css: allFiles
.filter((v) => v.endsWith('.css'))
.map((v) => v + getAssetQueryString()),
}
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ export default async function exportApp(
}
: {}),
strictNextHead: !!nextConfig.experimental.strictNextHead,
deploymentId: nextConfig.experimental.deploymentId,
}

const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ interface RenderOpts {
incrementalCache?: IncrementalCache
strictNextHead?: boolean
originalPathname?: string
deploymentId?: string
}

export default async function exportPage({
Expand Down Expand Up @@ -155,6 +156,9 @@ export default async function exportPage({
}

try {
if (renderOpts.deploymentId) {
process.env.__NEXT_DEPLOYMENT_ID = renderOpts.deploymentId
}
const { query: originalQuery = {} } = pathMap
const { page } = pathMap
const pathname = normalizeAppPath(page)
Expand Down
69 changes: 30 additions & 39 deletions packages/next/src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
const {
assetPrefix,
buildManifest,
devOnlyCacheBusterQueryString,
assetQueryString,
disableOptimizedLoading,
crossOrigin,
} = context
Expand All @@ -84,7 +84,7 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin}
noModule={true}
src={`${assetPrefix}/_next/${polyfill}${devOnlyCacheBusterQueryString}`}
src={`${assetPrefix}/_next/${polyfill}${assetQueryString}`}
/>
))
}
Expand Down Expand Up @@ -146,7 +146,7 @@ function getDynamicChunks(
dynamicImports,
assetPrefix,
isDevelopment,
devOnlyCacheBusterQueryString,
assetQueryString,
disableOptimizedLoading,
crossOrigin,
} = context
Expand All @@ -159,9 +159,7 @@ function getDynamicChunks(
async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading}
key={file}
src={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin}
/>
Expand All @@ -178,7 +176,7 @@ function getScripts(
assetPrefix,
buildManifest,
isDevelopment,
devOnlyCacheBusterQueryString,
assetQueryString,
disableOptimizedLoading,
crossOrigin,
} = context
Expand All @@ -192,9 +190,7 @@ function getScripts(
return (
<script
key={file}
src={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
nonce={props.nonce}
async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading}
Expand Down Expand Up @@ -358,7 +354,8 @@ function getAmpPath(ampPath: string, asPath: string): string {
function getNextFontLinkTags(
nextFontManifest: NextFontManifest | undefined,
dangerousAsPath: string,
assetPrefix: string = ''
assetPrefix: string = '',
assetQueryString: string = ''
) {
if (!nextFontManifest) {
return {
Expand All @@ -381,6 +378,11 @@ function getNextFontLinkTags(
(appFontsEntry || pageFontsEntry)
)

// we only add if the dpl query is present for fonts
if (!assetQueryString.includes('dpl=')) {
assetQueryString = ''
}

return {
preconnect: preconnectToSelf ? (
<link
Expand All @@ -399,7 +401,9 @@ function getNextFontLinkTags(
<link
key={fontFile}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(fontFile)}`}
href={`${assetPrefix}/_next/${encodeURI(
fontFile
)}${assetQueryString}`}
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
Expand All @@ -425,7 +429,7 @@ export class Head extends React.Component<HeadProps> {
getCssLinks(files: DocumentFiles): JSX.Element[] | null {
const {
assetPrefix,
devOnlyCacheBusterQueryString,
assetQueryString,
dynamicImports,
crossOrigin,
optimizeCss,
Expand Down Expand Up @@ -459,9 +463,7 @@ export class Head extends React.Component<HeadProps> {
key={`${file}-preload`}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
as="style"
crossOrigin={this.props.crossOrigin || crossOrigin}
/>
Expand All @@ -474,9 +476,7 @@ export class Head extends React.Component<HeadProps> {
key={file}
nonce={this.props.nonce}
rel="stylesheet"
href={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
crossOrigin={this.props.crossOrigin || crossOrigin}
data-n-g={isUnmanagedFile ? undefined : isSharedFile ? '' : undefined}
data-n-p={isUnmanagedFile ? undefined : isSharedFile ? undefined : ''}
Expand All @@ -494,12 +494,8 @@ export class Head extends React.Component<HeadProps> {
}

getPreloadDynamicChunks() {
const {
dynamicImports,
assetPrefix,
devOnlyCacheBusterQueryString,
crossOrigin,
} = this.context
const { dynamicImports, assetPrefix, assetQueryString, crossOrigin } =
this.context

return (
dynamicImports
Expand All @@ -514,7 +510,7 @@ export class Head extends React.Component<HeadProps> {
key={file}
href={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
)}${assetQueryString}`}
as="script"
nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || crossOrigin}
Expand All @@ -527,12 +523,8 @@ export class Head extends React.Component<HeadProps> {
}

getPreloadMainLinks(files: DocumentFiles): JSX.Element[] | null {
const {
assetPrefix,
devOnlyCacheBusterQueryString,
scriptLoader,
crossOrigin,
} = this.context
const { assetPrefix, assetQueryString, scriptLoader, crossOrigin } =
this.context
const preloadFiles = files.allFiles.filter((file: string) => {
return file.endsWith('.js')
})
Expand All @@ -553,9 +545,7 @@ export class Head extends React.Component<HeadProps> {
key={file}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
as="script"
crossOrigin={this.props.crossOrigin || crossOrigin}
/>
Expand Down Expand Up @@ -810,7 +800,8 @@ export class Head extends React.Component<HeadProps> {
const nextFontLinkTags = getNextFontLinkTags(
nextFontManifest,
dangerousAsPath,
assetPrefix
assetPrefix,
this.context.assetQueryString
)

return (
Expand Down Expand Up @@ -1073,7 +1064,7 @@ export class NextScript extends React.Component<OriginProps> {
buildManifest,
unstable_runtimeJS,
docComponentsRendered,
devOnlyCacheBusterQueryString,
assetQueryString,
disableOptimizedLoading,
crossOrigin,
} = this.context
Expand Down Expand Up @@ -1108,7 +1099,7 @@ export class NextScript extends React.Component<OriginProps> {
{ampDevFiles.map((file) => (
<script
key={file}
src={`${assetPrefix}/_next/${file}${devOnlyCacheBusterQueryString}`}
src={`${assetPrefix}/_next/${file}${assetQueryString}`}
nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || crossOrigin}
data-ampdevmode
Expand Down Expand Up @@ -1139,7 +1130,7 @@ export class NextScript extends React.Component<OriginProps> {
key={file}
src={`${assetPrefix}/_next/${encodeURI(
file
)}${devOnlyCacheBusterQueryString}`}
)}${assetQueryString}`}
nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || crossOrigin}
/>
Expand Down
Loading

0 comments on commit 04bbb69

Please sign in to comment.