Skip to content

Commit

Permalink
Fix standalone not found (#51172)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huozhi committed Jun 12, 2023
1 parent 8a11835 commit 9d0129c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
10 changes: 6 additions & 4 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1956,6 +1957,7 @@ const nextConfig = ${JSON.stringify({
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig)
createServerHandler({
port: currentPort,
hostname,
Expand Down
11 changes: 8 additions & 3 deletions packages/next/src/server/lib/render-server-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'],
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/app/dashboard/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function NotFound() {
return 'dashboard not found'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function page() {
return 'not-found-page-404'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
output: 'standalone',
}
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

0 comments on commit 9d0129c

Please sign in to comment.