Unverified Commit 9d0129c6 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix standalone not found (#51172)

Fixes #50232

Passing down the private env into `'render-server'` module to pick up the proper react with require-hook in standalone server
fix NEXT-1260
parent 8a11835f
...@@ -1922,20 +1922,21 @@ export async function copyTracedFiles( ...@@ -1922,20 +1922,21 @@ export async function copyTracedFiles(
serverOutputPath, serverOutputPath,
`${ `${
moduleType moduleType
? `import http from 'http' ? `\
import http from 'http'
import path from 'path' import path from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
import { createServerHandler } from 'next/dist/server/lib/render-server-standalone.js' import { createServerHandler } from 'next/dist/server/lib/render-server-standalone.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
` `
: ` : `\
const http = require('http') const http = require('http')
const path = require('path') const path = require('path')
const { createServerHandler } = require('next/dist/server/lib/render-server-standalone')` const { createServerHandler } = require('next/dist/server/lib/render-server-standalone')`
} }
const dir = path.join(__dirname) const dir = path.join(__dirname)
process.env.NODE_ENV = 'production' process.env.NODE_ENV = 'production'
process.chdir(__dirname) process.chdir(__dirname)
...@@ -1956,6 +1957,7 @@ const nextConfig = ${JSON.stringify({ ...@@ -1956,6 +1957,7 @@ const nextConfig = ${JSON.stringify({
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig) process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig)
createServerHandler({ createServerHandler({
port: currentPort, port: currentPort,
hostname, hostname,
......
...@@ -5,8 +5,6 @@ import httpProxy from 'next/dist/compiled/http-proxy' ...@@ -5,8 +5,6 @@ import httpProxy from 'next/dist/compiled/http-proxy'
import { Worker } from 'next/dist/compiled/jest-worker' import { Worker } from 'next/dist/compiled/jest-worker'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils' import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
const renderServerPath = require.resolve('./render-server')
export const createServerHandler = async ({ export const createServerHandler = async ({
port, port,
hostname, hostname,
...@@ -20,13 +18,20 @@ export const createServerHandler = async ({ ...@@ -20,13 +18,20 @@ export const createServerHandler = async ({
dev?: boolean dev?: boolean
minimalMode: 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, numWorkers: 1,
maxRetries: 10, maxRetries: 10,
forkOptions: { forkOptions: {
env: { env: {
FORCE_COLOR: '1', FORCE_COLOR: '1',
...process.env, ...process.env,
__NEXT_PRIVATE_PREBUNDLED_REACT: nextConfig?.experimental
?.useServerActions
? 'experimental'
: 'next',
}, },
}, },
exposedMethods: ['initialize'], exposedMethods: ['initialize'],
......
export default function NotFound() {
return 'dashboard not found'
}
export default function page() {
return 'not-found-page-404'
}
...@@ -127,4 +127,12 @@ describe('should set-up next', () => { ...@@ -127,4 +127,12 @@ describe('should set-up next', () => {
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() 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')
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment