From 943a6a9dd93388392b2b788ed3cc6a0bdac7eef1 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Wed, 23 Nov 2022 22:28:11 +0700 Subject: [PATCH 1/2] fix edge functions' nft files' wrong output path --- packages/next/build/webpack-config.ts | 1 + .../webpack/plugins/next-trace-entrypoints-plugin.ts | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 239ed636b00e..2e79d40668fc 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -2019,6 +2019,7 @@ export default async function getBaseWebpackConfig( appDirEnabled: hasAppDir, turbotrace: config.experimental.turbotrace, traceIgnores: config.experimental.outputFileTracingIgnores || [], + isEdgeServer, } ), // Moment.js is an extremely popular library that bundles large locale files diff --git a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts index 27c62ed25bd5..2044711878b6 100644 --- a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts +++ b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts @@ -111,6 +111,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { private chunksToTrace: string[] = [] private turbotraceOutputPath?: string private turbotraceFiles?: string[] + private isEdgeServer: boolean constructor({ appDir, @@ -119,6 +120,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { esmExternals, outputFileTracingRoot, turbotrace, + isEdgeServer, }: { appDir: string appDirEnabled?: boolean @@ -126,6 +128,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { outputFileTracingRoot?: string esmExternals?: NextConfigComplete['experimental']['esmExternals'] turbotrace?: NextConfigComplete['experimental']['turbotrace'] + isEdgeServer: boolean }) { this.appDir = appDir this.entryTraces = new Map() @@ -134,6 +137,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { this.traceIgnores = traceIgnores || [] this.tracingRoot = outputFileTracingRoot || appDir this.turbotrace = turbotrace + this.isEdgeServer = isEdgeServer } // Here we output all traced assets and webpack chunks to a @@ -236,7 +240,9 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { const parentFilesMap = getFilesMapFromReasons(fileList, reasons) for (const [entrypoint, entryFiles] of entryFilesMap) { - const traceOutputName = `../${entrypoint.name}.js.nft.json` + const traceOutputName = `${this.isEdgeServer ? '' : '../'}${ + entrypoint.name + }.js.nft.json` const traceOutputPath = nodePath.dirname( nodePath.join(outputPath, traceOutputName) ) From 471c91d6587f5ba935ea38ee570637088785f642 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 23 Nov 2022 18:20:31 -0800 Subject: [PATCH 2/2] Update fix and add test --- packages/next/build/webpack-config.ts | 3 +-- .../plugins/next-trace-entrypoints-plugin.ts | 8 +------ .../index.test.ts | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 2e79d40668fc..6aa11400fcac 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -2008,7 +2008,7 @@ export default async function getBaseWebpackConfig( }), (isClient || isEdgeServer) && new DropClientPage(), config.outputFileTracing && - (isNodeServer || isEdgeServer) && + isNodeServer && !dev && new (require('./webpack/plugins/next-trace-entrypoints-plugin') .TraceEntryPointsPlugin as typeof import('./webpack/plugins/next-trace-entrypoints-plugin').TraceEntryPointsPlugin)( @@ -2019,7 +2019,6 @@ export default async function getBaseWebpackConfig( appDirEnabled: hasAppDir, turbotrace: config.experimental.turbotrace, traceIgnores: config.experimental.outputFileTracingIgnores || [], - isEdgeServer, } ), // Moment.js is an extremely popular library that bundles large locale files diff --git a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts index 2044711878b6..27c62ed25bd5 100644 --- a/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts +++ b/packages/next/build/webpack/plugins/next-trace-entrypoints-plugin.ts @@ -111,7 +111,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { private chunksToTrace: string[] = [] private turbotraceOutputPath?: string private turbotraceFiles?: string[] - private isEdgeServer: boolean constructor({ appDir, @@ -120,7 +119,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { esmExternals, outputFileTracingRoot, turbotrace, - isEdgeServer, }: { appDir: string appDirEnabled?: boolean @@ -128,7 +126,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { outputFileTracingRoot?: string esmExternals?: NextConfigComplete['experimental']['esmExternals'] turbotrace?: NextConfigComplete['experimental']['turbotrace'] - isEdgeServer: boolean }) { this.appDir = appDir this.entryTraces = new Map() @@ -137,7 +134,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { this.traceIgnores = traceIgnores || [] this.tracingRoot = outputFileTracingRoot || appDir this.turbotrace = turbotrace - this.isEdgeServer = isEdgeServer } // Here we output all traced assets and webpack chunks to a @@ -240,9 +236,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { const parentFilesMap = getFilesMapFromReasons(fileList, reasons) for (const [entrypoint, entryFiles] of entryFilesMap) { - const traceOutputName = `${this.isEdgeServer ? '' : '../'}${ - entrypoint.name - }.js.nft.json` + const traceOutputName = `../${entrypoint.name}.js.nft.json` const traceOutputPath = nodePath.dirname( nodePath.join(outputPath, traceOutputName) ) diff --git a/test/e2e/edge-render-getserversideprops/index.test.ts b/test/e2e/edge-render-getserversideprops/index.test.ts index c3ea23aca7ed..8d6133490cae 100644 --- a/test/e2e/edge-render-getserversideprops/index.test.ts +++ b/test/e2e/edge-render-getserversideprops/index.test.ts @@ -4,6 +4,7 @@ import { fetchViaHTTP, normalizeRegEx, renderViaHTTP } from 'next-test-utils' import cheerio from 'cheerio' import { join } from 'path' import escapeStringRegexp from 'escape-string-regexp' +import fs from 'fs-extra' describe('edge-render-getserversideprops', () => { let next: NextInstance @@ -16,6 +17,28 @@ describe('edge-render-getserversideprops', () => { }) afterAll(() => next.destroy()) + if ((global as any).isNextStart) { + it('should not output trace files for edge routes', async () => { + expect(await fs.pathExists(join(next.testDir, '.next/pages'))).toBe(false) + expect( + await fs.pathExists(join(next.testDir, '.next/server/pages/[id].js')) + ).toBe(true) + expect( + await fs.pathExists( + join(next.testDir, '.next/server/pages/[id].js.nft.json') + ) + ).toBe(false) + expect( + await fs.pathExists(join(next.testDir, '.next/server/pages/index.js')) + ).toBe(true) + expect( + await fs.pathExists( + join(next.testDir, '.next/server/pages/index.js.nft.json') + ) + ).toBe(false) + }) + } + it('should have correct query/params on index', async () => { const html = await renderViaHTTP(next.url, '/') const $ = cheerio.load(html)