Skip to content

Commit

Permalink
build(edge): extract buildId into environment
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 15, 2024
1 parent 8680d2a commit a9f085d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,8 @@ export default async function build(
},
]

routes.forEach((route) => {
// Always sort the routes to get consistent output in manifests
getSortedRoutes(routes).forEach((route) => {
if (isDynamicRoute(page) && route === page) return
if (route === UNDERSCORE_NOT_FOUND_ROUTE) return

Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,10 @@ export default async function getBaseWebpackConfig(
dev,
sriEnabled: !dev && !!config.experimental.sri?.algorithm,
rewrites,
edgeEnvironments: edgePreviewProps || {},
edgeEnvironments: {
...edgePreviewProps,
buildId,
},
}),
isClient &&
new BuildManifestPlugin({
Expand Down
53 changes: 46 additions & 7 deletions packages/next/src/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ export type ClientBuildManifest = {
// generated).
export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`

// Return different path for edge runtime and nodejs runtime
// edge: '"/static/" + process.env.NEXT_BUILD_ID + "/low-priority.js"'
// nodejs: '/static/<build id>/low-priority.js'
function buildLowPriorityPath(
filename: string,
buildId: string,
isEdgeRuntime: boolean
) {
return isEdgeRuntime
? `"${CLIENT_STATIC_FILES_PATH}/" + process.env.NEXT_BUILD_ID + "/${filename}"`
: `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
}

function createEdgeRuntimeManifest(
originAssetMap: BuildManifest,
buildId: string
): string {
const assetMap = {
...originAssetMap,
lowPriorityFiles: [
buildLowPriorityPath('_buildManifest.js', buildId, true),
buildLowPriorityPath('_ssgManifest.js', buildId, true),
],
}

return JSON.stringify(assetMap, null, 2)
}

function normalizeRewrite(item: {
source: string
destination: string
Expand Down Expand Up @@ -231,19 +259,27 @@ export default class BuildManifestPlugin {
// Add the runtime build manifest file (generated later in this file)
// as a dependency for the app. If the flag is false, the file won't be
// downloaded by the client.
assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
const buildManifestPath = buildLowPriorityPath(
'_buildManifest.js',
this.buildId,
false
)
const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`

assetMap.lowPriorityFiles.push(ssgManifestPath)
const ssgManifestPath = buildLowPriorityPath(
'_ssgManifest.js',
this.buildId,
false
)
assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath)
assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest)
}

assetMap.pages = Object.keys(assetMap.pages)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)
.reduce(
(a, c) => ((a[c] = assetMap.pages[c]), a),
{} as typeof assetMap.pages
)

let buildManifestName = BUILD_MANIFEST

Expand All @@ -256,7 +292,10 @@ export default class BuildManifestPlugin {
)

assets[`server/${MIDDLEWARE_BUILD_MANIFEST}.js`] = new sources.RawSource(
`self.__BUILD_MANIFEST=${JSON.stringify(assetMap)}`
`self.__BUILD_MANIFEST=${createEdgeRuntimeManifest(
assetMap,
this.buildId
)}`
)

if (!this.isDevFallback) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('Middleware Runtime', () => {
regions: 'auto',
})
expect(envs).toContainAllKeys([
'buildId',
'previewModeEncryptionKey',
'previewModeId',
'previewModeSigningKey',
Expand Down

0 comments on commit a9f085d

Please sign in to comment.