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..32b66022dd2d9 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,13 +18,20 @@ export const createServerHandler = async ({ dev?: boolean minimalMode: boolean }) => { - const routerWorker = new Worker(renderServerPath, { + const nextConfig = JSON.parse( + process.env.__NEXT_PRIVATE_STANDALONE_CONFIG || '{}' + ) + const routerWorker = new Worker(require.resolve('./render-server'), { numWorkers: 1, maxRetries: 10, forkOptions: { env: { FORCE_COLOR: '1', ...process.env, + __NEXT_PRIVATE_PREBUNDLED_REACT: nextConfig?.experimental + ?.useServerActions + ? 'experimental' + : 'next', }, }, exposedMethods: ['initialize'], 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.js b/test/production/standalone-mode/required-server-files/app/not-found.js new file mode 100644 index 0000000000000..134cec4f3360b --- /dev/null +++ b/test/production/standalone-mode/required-server-files/app/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/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') + }) })