From 6c4f004eb4a90b07437815040412b4d55931747e Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 11 Jun 2023 11:06:27 +0200 Subject: [PATCH 1/2] Fix standalone not found --- packages/next/src/build/utils.ts | 10 ++++++---- .../next/src/server/lib/render-server-standalone.ts | 4 +--- test/e2e/app-dir/app/app/dashboard/not-found.js | 3 +++ .../required-server-files/app/not-found/not-found.js | 3 +++ .../required-server-files/app/not-found/page.js | 3 +++ .../required-server-files/next.config.js | 3 +++ .../required-server-files-app.test.ts | 8 ++++++++ 7 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 test/e2e/app-dir/app/app/dashboard/not-found.js create mode 100644 test/production/standalone-mode/required-server-files/app/not-found/not-found.js create mode 100644 test/production/standalone-mode/required-server-files/app/not-found/page.js create mode 100644 test/production/standalone-mode/required-server-files/next.config.js diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index cbb7a221e4ed7..a47a8c83cfb86 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -1922,20 +1922,21 @@ export async function copyTracedFiles( serverOutputPath, `${ moduleType - ? `import http from 'http' + ? `\ +import http from 'http' import path from 'path' import { fileURLToPath } from 'url' -const __dirname = fileURLToPath(new URL('.', import.meta.url)) import { createServerHandler } from 'next/dist/server/lib/render-server-standalone.js' + +const __dirname = fileURLToPath(new URL('.', import.meta.url)) ` - : ` + : `\ const http = require('http') const path = require('path') const { createServerHandler } = require('next/dist/server/lib/render-server-standalone')` } const dir = path.join(__dirname) - process.env.NODE_ENV = 'production' process.chdir(__dirname) @@ -1956,6 +1957,7 @@ const nextConfig = ${JSON.stringify({ process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig) + createServerHandler({ port: currentPort, hostname, diff --git a/packages/next/src/server/lib/render-server-standalone.ts b/packages/next/src/server/lib/render-server-standalone.ts index bbfb406772186..20ca4845b1466 100644 --- a/packages/next/src/server/lib/render-server-standalone.ts +++ b/packages/next/src/server/lib/render-server-standalone.ts @@ -5,8 +5,6 @@ import httpProxy from 'next/dist/compiled/http-proxy' import { Worker } from 'next/dist/compiled/jest-worker' import { normalizeRepeatedSlashes } from '../../shared/lib/utils' -const renderServerPath = require.resolve('./render-server') - export const createServerHandler = async ({ port, hostname, @@ -20,7 +18,7 @@ export const createServerHandler = async ({ dev?: boolean minimalMode: boolean }) => { - const routerWorker = new Worker(renderServerPath, { + const routerWorker = new Worker(require.resolve('./render-server'), { numWorkers: 1, maxRetries: 10, forkOptions: { diff --git a/test/e2e/app-dir/app/app/dashboard/not-found.js b/test/e2e/app-dir/app/app/dashboard/not-found.js new file mode 100644 index 0000000000000..bab4962f90716 --- /dev/null +++ b/test/e2e/app-dir/app/app/dashboard/not-found.js @@ -0,0 +1,3 @@ +export default function NotFound() { + return 'dashboard not found' +} diff --git a/test/production/standalone-mode/required-server-files/app/not-found/not-found.js b/test/production/standalone-mode/required-server-files/app/not-found/not-found.js new file mode 100644 index 0000000000000..134cec4f3360b --- /dev/null +++ b/test/production/standalone-mode/required-server-files/app/not-found/not-found.js @@ -0,0 +1,3 @@ +export default function page() { + return 'not-found-page-404' +} diff --git a/test/production/standalone-mode/required-server-files/app/not-found/page.js b/test/production/standalone-mode/required-server-files/app/not-found/page.js new file mode 100644 index 0000000000000..22593ab14b135 --- /dev/null +++ b/test/production/standalone-mode/required-server-files/app/not-found/page.js @@ -0,0 +1,3 @@ +export default function page() { + return 'not-found-page-200' +} diff --git a/test/production/standalone-mode/required-server-files/next.config.js b/test/production/standalone-mode/required-server-files/next.config.js new file mode 100644 index 0000000000000..e97173b4b3799 --- /dev/null +++ b/test/production/standalone-mode/required-server-files/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + output: 'standalone', +} diff --git a/test/production/standalone-mode/required-server-files/required-server-files-app.test.ts b/test/production/standalone-mode/required-server-files/required-server-files-app.test.ts index 9cfd3a69a5524..2e2e31eb06fd1 100644 --- a/test/production/standalone-mode/required-server-files/required-server-files-app.test.ts +++ b/test/production/standalone-mode/required-server-files/required-server-files-app.test.ts @@ -127,4 +127,12 @@ describe('should set-up next', () => { expect(res.headers.get('x-next-cache-tags')).toBeFalsy() } }) + + it('should handle correctly not-found.js', async () => { + const res = await fetchViaHTTP(appPort, '/not-found/does-not-exist') + expect(res.status).toBe(404) + const html = await res.text() + expect(html).toContain('not-found-page-404') + expect(html).not.toContain('not-found-page-200') + }) }) From c2fb4f771f49df1024daabf904f50df61f859053 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 12 Jun 2023 15:02:58 +0200 Subject: [PATCH 2/2] update tests --- packages/next/src/server/lib/render-server-standalone.ts | 7 +++++++ .../required-server-files/app/{not-found => }/not-found.js | 0 .../required-server-files/app/not-found/page.js | 3 --- 3 files changed, 7 insertions(+), 3 deletions(-) rename test/production/standalone-mode/required-server-files/app/{not-found => }/not-found.js (100%) delete mode 100644 test/production/standalone-mode/required-server-files/app/not-found/page.js diff --git a/packages/next/src/server/lib/render-server-standalone.ts b/packages/next/src/server/lib/render-server-standalone.ts index 20ca4845b1466..32b66022dd2d9 100644 --- a/packages/next/src/server/lib/render-server-standalone.ts +++ b/packages/next/src/server/lib/render-server-standalone.ts @@ -18,6 +18,9 @@ export const createServerHandler = async ({ dev?: boolean minimalMode: boolean }) => { + const nextConfig = JSON.parse( + process.env.__NEXT_PRIVATE_STANDALONE_CONFIG || '{}' + ) const routerWorker = new Worker(require.resolve('./render-server'), { numWorkers: 1, maxRetries: 10, @@ -25,6 +28,10 @@ export const createServerHandler = async ({ env: { FORCE_COLOR: '1', ...process.env, + __NEXT_PRIVATE_PREBUNDLED_REACT: nextConfig?.experimental + ?.useServerActions + ? 'experimental' + : 'next', }, }, exposedMethods: ['initialize'], diff --git a/test/production/standalone-mode/required-server-files/app/not-found/not-found.js b/test/production/standalone-mode/required-server-files/app/not-found.js similarity index 100% rename from test/production/standalone-mode/required-server-files/app/not-found/not-found.js rename to test/production/standalone-mode/required-server-files/app/not-found.js diff --git a/test/production/standalone-mode/required-server-files/app/not-found/page.js b/test/production/standalone-mode/required-server-files/app/not-found/page.js deleted file mode 100644 index 22593ab14b135..0000000000000 --- a/test/production/standalone-mode/required-server-files/app/not-found/page.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function page() { - return 'not-found-page-200' -}