From 6153876c2fb524658423f315a5292e1fd260f924 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Thu, 24 Nov 2022 16:14:03 +0700 Subject: [PATCH 1/5] Fix `Failed to copy traced files` for Edge functions --- packages/next/build/utils.ts | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index ea5c9b929d14..39a4bed24bfc 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1669,7 +1669,37 @@ export async function copyTracedFiles( } } + const edgeRuntimePages = new Set( + Object.keys(middlewareManifest.functions) + ) + + 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 (edgeRuntimePages.has(page)) { + continue + } const pageFile = path.join( distDir, 'server', @@ -1683,6 +1713,9 @@ export async function copyTracedFiles( } if (appPageKeys) { for (const page of appPageKeys) { + if (edgeRuntimePages.has(page)) { + continue + } const pageFile = path.join(distDir, 'server', 'app', `${page}`, 'page.js') const pageTraceFile = `${pageFile}.nft.json` await handleTraceFiles(pageTraceFile).catch((err) => { @@ -1760,6 +1793,28 @@ server.listen(currentPort, (err) => { })` ) } + +/*export async function copyFilesForMiddlewareAndEdgeFunctions( + distDir: string, + middlewareManifest: MiddlewareManifest, + edgeRuntimePages: string[] +) { + const outputPath = path.join(distDir, 'standalone') + const assetsKeys = ['wasm', 'assets'] as const + const filesKeys = 'files' + const b = middlewareManifest.functions['hehe'][assetsKeys[0]] + for (const path of Object.keys(middlewareManifest.functions)) { + for (const assetsKey of assetsKeys) { + const assets = middlewareManifest.functions[path][assetsKey] + if (assets) { + for (const asset of assets) { + + } + } + } + } +} */ + export function isReservedPage(page: string) { return RESERVED_PAGE.test(page) } From 3d680a37950e96f2fd3ad052c6b9a28409fd0967 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Thu, 24 Nov 2022 16:26:26 +0700 Subject: [PATCH 2/5] remove unnecessary comment --- packages/next/build/utils.ts | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 39a4bed24bfc..11f9785579b3 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1794,27 +1794,6 @@ server.listen(currentPort, (err) => { ) } -/*export async function copyFilesForMiddlewareAndEdgeFunctions( - distDir: string, - middlewareManifest: MiddlewareManifest, - edgeRuntimePages: string[] -) { - const outputPath = path.join(distDir, 'standalone') - const assetsKeys = ['wasm', 'assets'] as const - const filesKeys = 'files' - const b = middlewareManifest.functions['hehe'][assetsKeys[0]] - for (const path of Object.keys(middlewareManifest.functions)) { - for (const assetsKey of assetsKeys) { - const assets = middlewareManifest.functions[path][assetsKey] - if (assets) { - for (const asset of assets) { - - } - } - } - } -} */ - export function isReservedPage(page: string) { return RESERVED_PAGE.test(page) } From 384b6d668af4d5a60aa00079793f1ddd0814a7b1 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Fri, 25 Nov 2022 10:53:50 +0700 Subject: [PATCH 3/5] replaced edgeRuntimePages with hasOwnProperty --- packages/next/build/utils.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 11f9785579b3..eb9670a638c6 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1669,10 +1669,6 @@ export async function copyTracedFiles( } } - const edgeRuntimePages = new Set( - Object.keys(middlewareManifest.functions) - ) - for (const page of Object.values(middlewareManifest.functions)) { for (const file of page.files) { const originalPath = path.join(distDir, file) @@ -1697,7 +1693,7 @@ export async function copyTracedFiles( } for (const page of pageKeys) { - if (edgeRuntimePages.has(page)) { + if (middlewareManifest.functions.hasOwnProperty(page)) { continue } const pageFile = path.join( @@ -1713,7 +1709,7 @@ export async function copyTracedFiles( } if (appPageKeys) { for (const page of appPageKeys) { - if (edgeRuntimePages.has(page)) { + if (middlewareManifest.functions.hasOwnProperty(page)) { continue } const pageFile = path.join(distDir, 'server', 'app', `${page}`, 'page.js') From e11c4ab4c446903833a9722dbc9240469bd98e61 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Wed, 30 Nov 2022 01:14:29 +0700 Subject: [PATCH 4/5] extra: remove appManifestPath --- packages/next/build/index.ts | 8 ++------ 1 file changed, 2 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( From a0fdeac6c50062e4e15ddf545b9f4a4d273e481f Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 30 Nov 2022 19:08:09 -0800 Subject: [PATCH 5/5] Add test --- test/e2e/og-api/index.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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) + }) + } })