From 490969e0a6d6ad0e35fc6317e6be9f866f152d4e Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 16 Aug 2024 14:27:16 +0200 Subject: [PATCH 1/2] Fix flying-shuttle full mode breaking instrumentation --- packages/next/src/build/webpack-config.ts | 7 +++-- .../flying-shuttle/app/edge/page.js | 5 +++ .../flying-shuttle/app/layout.js | 7 +++++ .../flying-shuttle/app/page.js | 5 +++ .../flying-shuttle/flying-shuttle.test.ts | 31 +++++++++++++++++++ .../flying-shuttle/instrumentation.js | 3 ++ .../flying-shuttle/next.config.js | 11 +++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/app/edge/page.js create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/app/layout.js create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/app/page.js create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/flying-shuttle.test.ts create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js create mode 100644 test/e2e/instrumentation-hook/flying-shuttle/next.config.js diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 151d3c736a85a3..82d85539735445 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -1155,9 +1155,10 @@ export default async function getBaseWebpackConfig( return `static/chunks/[name]-[contenthash].js` }, - path: isNodeServer - ? path.join(outputPath, `chunks-${buildId}`) - : outputPath, + path: + !dev && isNodeServer + ? path.join(outputPath, `chunks-${buildId}`) + : outputPath, chunkFilename: isNodeOrEdgeCompilation ? `[name].js` diff --git a/test/e2e/instrumentation-hook/flying-shuttle/app/edge/page.js b/test/e2e/instrumentation-hook/flying-shuttle/app/edge/page.js new file mode 100644 index 00000000000000..cbd065a63938a6 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/app/edge/page.js @@ -0,0 +1,5 @@ +export default function Page() { + return 'edge' +} + +export const runtime = 'edge' diff --git a/test/e2e/instrumentation-hook/flying-shuttle/app/layout.js b/test/e2e/instrumentation-hook/flying-shuttle/app/layout.js new file mode 100644 index 00000000000000..750eb927b19801 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/app/layout.js @@ -0,0 +1,7 @@ +export default function Layout({ children }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/instrumentation-hook/flying-shuttle/app/page.js b/test/e2e/instrumentation-hook/flying-shuttle/app/page.js new file mode 100644 index 00000000000000..4d2f1cf3f287a1 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/app/page.js @@ -0,0 +1,5 @@ +export default function Page() { + return 'node' +} + +export const dynamic = 'force-dynamic' diff --git a/test/e2e/instrumentation-hook/flying-shuttle/flying-shuttle.test.ts b/test/e2e/instrumentation-hook/flying-shuttle/flying-shuttle.test.ts new file mode 100644 index 00000000000000..45f25aa5a1d878 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/flying-shuttle.test.ts @@ -0,0 +1,31 @@ +import fs from 'fs-extra' +import { nextTestSetup } from 'e2e-utils' +import { retry } from 'next-test-utils' + +describe('instrumentation-hook - flying-shuttle', () => { + const { next, skipped } = nextTestSetup({ + files: __dirname, + skipDeployment: true, + }) + + if (skipped) { + return + } + + beforeAll(async () => { + await fs.remove('.next') + }) + + it('should only register without errors', async () => { + await next.fetch('/') + await next.fetch('/edge') + + await retry(() => { + expect(next.cliOutput).toIncludeRepeated('register:edge', 1) + expect(next.cliOutput).not.toContain( + 'An error occurred while loading instrumentation hook' + ) + expect(next.cliOutput).toIncludeRepeated('register:nodejs', 1) + }) + }) +}) diff --git a/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js b/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js new file mode 100644 index 00000000000000..57e3319c6f1aa5 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js @@ -0,0 +1,3 @@ +export function register() { + console.log('register:' + process.env.NEXT_RUNTIME) +} diff --git a/test/e2e/instrumentation-hook/flying-shuttle/next.config.js b/test/e2e/instrumentation-hook/flying-shuttle/next.config.js new file mode 100644 index 00000000000000..182b9640127df7 --- /dev/null +++ b/test/e2e/instrumentation-hook/flying-shuttle/next.config.js @@ -0,0 +1,11 @@ +/** + * @type {import('next').NextConfig} + */ +module.exports = { + experimental: { + flyingShuttle: { + mode: 'full', + }, + instrumentationHook: true, + }, +} From ed52baf9bfa35d6e4828f9d8f78cfbf2933ef85a Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 16 Aug 2024 14:45:10 +0200 Subject: [PATCH 2/2] ensure build mode is not invoking register instrumentation --- packages/next/src/server/web/globals.ts | 2 ++ test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/next/src/server/web/globals.ts b/packages/next/src/server/web/globals.ts index 6c4bf22ef2e8c5..401396750574c0 100644 --- a/packages/next/src/server/web/globals.ts +++ b/packages/next/src/server/web/globals.ts @@ -18,6 +18,8 @@ export async function getEdgeInstrumentationModule(): Promise< let instrumentationModulePromise: Promise | null = null async function registerInstrumentation() { + // Ensure registerInstrumentation is not called in production build + if (process.env.NEXT_PHASE === 'phase-production-build') return if (!instrumentationModulePromise) { instrumentationModulePromise = getEdgeInstrumentationModule() } diff --git a/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js b/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js index 57e3319c6f1aa5..0c1706c8805e09 100644 --- a/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js +++ b/test/e2e/instrumentation-hook/flying-shuttle/instrumentation.js @@ -1,3 +1,3 @@ export function register() { - console.log('register:' + process.env.NEXT_RUNTIME) + console.trace('register:' + process.env.NEXT_RUNTIME) }