From f6106d20fb2cf5a3e634111a2b1b9cc00f188daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20=C4=90=E1=BB=A9c=20Anh?= <75556609+DuCanhGH@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:35:59 +0700 Subject: [PATCH] Fix `Failed to copy traced files` for Edge functions and handle its files with middleware-manifest.json (#43326) ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) fixes: #41395 fixes: #42751. This PR is a follow-up PR of [PR 43304](https://github.com/vercel/next.js/pull/43304). This fix works by checking if the page is listed in middleware-manifest.json's functions, if true then skip the handleTraceFiles process. This also fixes the two issues aforementioned by copying files listed in middleware-manifest.json for those pages. Co-authored-by: JJ Kasper --- packages/next/build/index.ts | 8 ++------ packages/next/build/utils.ts | 30 ++++++++++++++++++++++++++++++ test/e2e/og-api/index.test.ts | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 7d11b8975fa1..6887cd2865b1 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -868,11 +868,6 @@ export default async function build( ) const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST) - const appManifestPath = path.join( - distDir, - SERVER_DIRECTORY, - APP_PATHS_MANIFEST - ) const requiredServerFiles = nextBuildSpan .traceChild('generate-required-server-files') @@ -912,7 +907,8 @@ export default async function build( ), ] : []), - path.relative(distDir, appManifestPath), + path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST), + APP_BUILD_MANIFEST, path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.js'), path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.json'), path.join( diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index ea5c9b929d14..eb9670a638c6 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1669,7 +1669,33 @@ export async function copyTracedFiles( } } + for (const page of Object.values(middlewareManifest.functions)) { + for (const file of page.files) { + const originalPath = path.join(distDir, file) + const fileOutputPath = path.join( + outputPath, + path.relative(tracingRoot, distDir), + file + ) + await fs.mkdir(path.dirname(fileOutputPath), { recursive: true }) + await fs.copyFile(originalPath, fileOutputPath) + } + for (const file of [...(page.wasm ?? []), ...(page.assets ?? [])]) { + const originalPath = path.join(distDir, file.filePath) + const fileOutputPath = path.join( + outputPath, + path.relative(tracingRoot, distDir), + file.filePath + ) + await fs.mkdir(path.dirname(fileOutputPath), { recursive: true }) + await fs.copyFile(originalPath, fileOutputPath) + } + } + for (const page of pageKeys) { + if (middlewareManifest.functions.hasOwnProperty(page)) { + continue + } const pageFile = path.join( distDir, 'server', @@ -1683,6 +1709,9 @@ export async function copyTracedFiles( } if (appPageKeys) { for (const page of appPageKeys) { + if (middlewareManifest.functions.hasOwnProperty(page)) { + continue + } const pageFile = path.join(distDir, 'server', 'app', `${page}`, 'page.js') const pageTraceFile = `${pageFile}.nft.json` await handleTraceFiles(pageTraceFile).catch((err) => { @@ -1760,6 +1789,7 @@ server.listen(currentPort, (err) => { })` ) } + export function isReservedPage(page: string) { return RESERVED_PAGE.test(page) } diff --git a/test/e2e/og-api/index.test.ts b/test/e2e/og-api/index.test.ts index 9da3b614cef0..038728e4d829 100644 --- a/test/e2e/og-api/index.test.ts +++ b/test/e2e/og-api/index.test.ts @@ -1,6 +1,7 @@ import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' +import fs from 'fs-extra' import { join } from 'path' describe('og-api', () => { @@ -27,4 +28,21 @@ describe('og-api', () => { const body = await res.blob() expect(body.size).toBeGreaterThan(0) }) + + if ((global as any).isNextStart) { + it('should copy files correctly', async () => { + expect(next.cliOutput).not.toContain('Failed to copy traced files') + + expect( + await fs.pathExists( + join(next.testDir, '.next/standalone/.next/server/pages/api/og.js') + ) + ).toBe(true) + expect( + await fs.pathExists( + join(next.testDir, '.next/standalone/.next/server/edge-chunks') + ) + ).toBe(true) + }) + } })