Unverified Commit 5aaeb851 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add writeManifest (#60138)

## What?

Follow-up to `readManifest` this adds `writeManifest`.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-1955
parent 25d7e3d8
...@@ -393,14 +393,21 @@ function getNumberOfWorkers(config: NextConfigComplete) { ...@@ -393,14 +393,21 @@ function getNumberOfWorkers(config: NextConfigComplete) {
return 4 return 4
} }
async function writeFileUtf8(filePath: string, content: string) { async function writeFileUtf8(filePath: string, content: string): Promise<void> {
await fs.writeFile(filePath, content, 'utf-8') await fs.writeFile(filePath, content, 'utf-8')
} }
function readFileUtf8(filePath: string) { function readFileUtf8(filePath: string): Promise<string> {
return fs.readFile(filePath, 'utf8') return fs.readFile(filePath, 'utf8')
} }
async function writeManifest<T extends object>(
filePath: string,
manifest: T
): Promise<void> {
await writeFileUtf8(filePath, formatManifest(manifest))
}
async function readManifest<T extends object>(filePath: string): Promise<T> { async function readManifest<T extends object>(filePath: string): Promise<T> {
return JSON.parse(await readFileUtf8(filePath)) return JSON.parse(await readFileUtf8(filePath))
} }
...@@ -957,9 +964,7 @@ export default async function build( ...@@ -957,9 +964,7 @@ export default async function build(
// We need to write the manifest with rewrites before build // We need to write the manifest with rewrites before build
await nextBuildSpan await nextBuildSpan
.traceChild('write-routes-manifest') .traceChild('write-routes-manifest')
.traceAsyncFn(() => .traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest))
writeFileUtf8(routesManifestPath, formatManifest(routesManifest))
)
// We need to write a partial prerender manifest to make preview mode settings available in edge middleware // We need to write a partial prerender manifest to make preview mode settings available in edge middleware
const partialManifest: Partial<PrerenderManifest> = { const partialManifest: Partial<PrerenderManifest> = {
...@@ -1250,18 +1255,16 @@ export default async function build( ...@@ -1250,18 +1255,16 @@ export default async function build(
const appPathRoutes: Record<string, string> = {} const appPathRoutes: Record<string, string> = {}
if (appDir) { if (appDir) {
appPathsManifest = JSON.parse( appPathsManifest = await readManifest(
await readFileUtf8( path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST)
path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST)
)
) )
Object.keys(appPathsManifest).forEach((entry) => { Object.keys(appPathsManifest).forEach((entry) => {
appPathRoutes[entry] = normalizeAppPath(entry) appPathRoutes[entry] = normalizeAppPath(entry)
}) })
await writeFileUtf8( await writeManifest(
path.join(distDir, APP_PATH_ROUTES_MANIFEST), path.join(distDir, APP_PATH_ROUTES_MANIFEST),
formatManifest(appPathRoutes) appPathRoutes
) )
} }
...@@ -1950,9 +1953,9 @@ export default async function build( ...@@ -1950,9 +1953,9 @@ export default async function build(
functions: functionsConfigManifest, functions: functionsConfigManifest,
} }
await writeFileUtf8( await writeManifest(
path.join(distDir, SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST), path.join(distDir, SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST),
formatManifest(manifest) manifest
) )
} }
...@@ -1983,7 +1986,7 @@ export default async function build( ...@@ -1983,7 +1986,7 @@ export default async function build(
return buildDataRoute(page, buildId) return buildDataRoute(page, buildId)
}) })
await writeFileUtf8(routesManifestPath, formatManifest(routesManifest)) await writeManifest(routesManifestPath, routesManifest)
} }
// Since custom _app.js can wrap the 404 page we have to opt-out of static optimization if it has getInitialProps // Since custom _app.js can wrap the 404 page we have to opt-out of static optimization if it has getInitialProps
...@@ -2054,9 +2057,9 @@ export default async function build( ...@@ -2054,9 +2057,9 @@ export default async function build(
}) })
) )
await writeFileUtf8( await writeManifest(
path.join(distDir, SERVER_FILES_MANIFEST), path.join(distDir, SERVER_FILES_MANIFEST),
formatManifest(requiredServerFiles) requiredServerFiles
) )
const middlewareManifest: MiddlewareManifest = await readManifest( const middlewareManifest: MiddlewareManifest = await readManifest(
...@@ -2789,7 +2792,7 @@ export default async function build( ...@@ -2789,7 +2792,7 @@ export default async function build(
// remove temporary export folder // remove temporary export folder
await fs.rm(exportOptions.outdir, { recursive: true, force: true }) await fs.rm(exportOptions.outdir, { recursive: true, force: true })
await writeFileUtf8(pagesManifestPath, formatManifest(pagesManifest)) await writeManifest(pagesManifestPath, pagesManifest)
}) })
} }
...@@ -2885,9 +2888,9 @@ export default async function build( ...@@ -2885,9 +2888,9 @@ export default async function build(
NextBuildContext.allowedRevalidateHeaderKeys = NextBuildContext.allowedRevalidateHeaderKeys =
config.experimental.allowedRevalidateHeaderKeys config.experimental.allowedRevalidateHeaderKeys
await writeFileUtf8( await writeManifest(
path.join(distDir, PRERENDER_MANIFEST), path.join(distDir, PRERENDER_MANIFEST),
formatManifest(prerenderManifest) prerenderManifest
) )
await writeFileUtf8( await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'), path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
...@@ -2908,9 +2911,9 @@ export default async function build( ...@@ -2908,9 +2911,9 @@ export default async function build(
preview: previewProps, preview: previewProps,
notFoundRoutes: [], notFoundRoutes: [],
} }
await writeFileUtf8( await writeManifest(
path.join(distDir, PRERENDER_MANIFEST), path.join(distDir, PRERENDER_MANIFEST),
formatManifest(prerenderManifest) prerenderManifest
) )
await writeFileUtf8( await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'), path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
...@@ -2933,22 +2936,16 @@ export default async function build( ...@@ -2933,22 +2936,16 @@ export default async function build(
}) })
) )
await writeFileUtf8( await writeManifest(path.join(distDir, IMAGES_MANIFEST), {
path.join(distDir, IMAGES_MANIFEST), version: 1,
formatManifest({ images,
version: 1, })
images, await writeManifest(path.join(distDir, EXPORT_MARKER), {
}) version: 1,
) hasExportPathMap: typeof config.exportPathMap === 'function',
await writeFileUtf8( exportTrailingSlash: config.trailingSlash === true,
path.join(distDir, EXPORT_MARKER), isNextImageImported: isNextImageImported === true,
formatManifest({ })
version: 1,
hasExportPathMap: typeof config.exportPathMap === 'function',
exportTrailingSlash: config.trailingSlash === true,
isNextImageImported: isNextImageImported === true,
})
)
await fs.unlink(path.join(distDir, EXPORT_DETAIL)).catch((err) => { await fs.unlink(path.join(distDir, EXPORT_DETAIL)).catch((err) => {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
return Promise.resolve() return Promise.resolve()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment