Skip to content

Commit

Permalink
Add writeManifest (vercel#60138)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
timneutkens authored and agustints committed Jan 6, 2024
1 parent 3052d60 commit 2cceeba
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,21 @@ function getNumberOfWorkers(config: NextConfigComplete) {
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')
}

function readFileUtf8(filePath: string) {
function readFileUtf8(filePath: string): Promise<string> {
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> {
return JSON.parse(await readFileUtf8(filePath))
}
Expand Down Expand Up @@ -957,9 +964,7 @@ export default async function build(
// We need to write the manifest with rewrites before build
await nextBuildSpan
.traceChild('write-routes-manifest')
.traceAsyncFn(() =>
writeFileUtf8(routesManifestPath, formatManifest(routesManifest))
)
.traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest))

// We need to write a partial prerender manifest to make preview mode settings available in edge middleware
const partialManifest: Partial<PrerenderManifest> = {
Expand Down Expand Up @@ -1250,18 +1255,16 @@ export default async function build(
const appPathRoutes: Record<string, string> = {}

if (appDir) {
appPathsManifest = JSON.parse(
await readFileUtf8(
path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST)
)
appPathsManifest = await readManifest(
path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST)
)

Object.keys(appPathsManifest).forEach((entry) => {
appPathRoutes[entry] = normalizeAppPath(entry)
})
await writeFileUtf8(
await writeManifest(
path.join(distDir, APP_PATH_ROUTES_MANIFEST),
formatManifest(appPathRoutes)
appPathRoutes
)
}

Expand Down Expand Up @@ -1950,9 +1953,9 @@ export default async function build(
functions: functionsConfigManifest,
}

await writeFileUtf8(
await writeManifest(
path.join(distDir, SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST),
formatManifest(manifest)
manifest
)
}

Expand Down Expand Up @@ -1983,7 +1986,7 @@ export default async function build(
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
Expand Down Expand Up @@ -2054,9 +2057,9 @@ export default async function build(
})
)

await writeFileUtf8(
await writeManifest(
path.join(distDir, SERVER_FILES_MANIFEST),
formatManifest(requiredServerFiles)
requiredServerFiles
)

const middlewareManifest: MiddlewareManifest = await readManifest(
Expand Down Expand Up @@ -2789,7 +2792,7 @@ export default async function build(

// remove temporary export folder
await fs.rm(exportOptions.outdir, { recursive: true, force: true })
await writeFileUtf8(pagesManifestPath, formatManifest(pagesManifest))
await writeManifest(pagesManifestPath, pagesManifest)
})
}

Expand Down Expand Up @@ -2885,9 +2888,9 @@ export default async function build(
NextBuildContext.allowedRevalidateHeaderKeys =
config.experimental.allowedRevalidateHeaderKeys

await writeFileUtf8(
await writeManifest(
path.join(distDir, PRERENDER_MANIFEST),
formatManifest(prerenderManifest)
prerenderManifest
)
await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
Expand All @@ -2908,9 +2911,9 @@ export default async function build(
preview: previewProps,
notFoundRoutes: [],
}
await writeFileUtf8(
await writeManifest(
path.join(distDir, PRERENDER_MANIFEST),
formatManifest(prerenderManifest)
prerenderManifest
)
await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
Expand All @@ -2933,22 +2936,16 @@ export default async function build(
})
)

await writeFileUtf8(
path.join(distDir, IMAGES_MANIFEST),
formatManifest({
version: 1,
images,
})
)
await writeFileUtf8(
path.join(distDir, EXPORT_MARKER),
formatManifest({
version: 1,
hasExportPathMap: typeof config.exportPathMap === 'function',
exportTrailingSlash: config.trailingSlash === true,
isNextImageImported: isNextImageImported === true,
})
)
await writeManifest(path.join(distDir, IMAGES_MANIFEST), {
version: 1,
images,
})
await writeManifest(path.join(distDir, EXPORT_MARKER), {
version: 1,
hasExportPathMap: typeof config.exportPathMap === 'function',
exportTrailingSlash: config.trailingSlash === true,
isNextImageImported: isNextImageImported === true,
})
await fs.unlink(path.join(distDir, EXPORT_DETAIL)).catch((err) => {
if (err.code === 'ENOENT') {
return Promise.resolve()
Expand Down

0 comments on commit 2cceeba

Please sign in to comment.