-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix standalone mode with appDir running in a single process (#49116)
Fixes #49055, fixes #48918. App dir will always require the server to run in the workers mode, so it can keep a separate Node.js process for pages. This PR updates the standalone server to initialize a "standalone server" (which works similar to `start-server`), and changes the tracked files to include Jest worker.
- Loading branch information
Showing
17 changed files
with
217 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/next/src/server/lib/render-server-standalone.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { IncomingMessage, ServerResponse } from 'http' | ||
import type { ChildProcess } from 'child_process' | ||
|
||
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, | ||
dir, | ||
minimalMode, | ||
}: { | ||
port: number | ||
hostname: string | ||
dir: string | ||
minimalMode: boolean | ||
}) => { | ||
const routerWorker = new Worker(renderServerPath, { | ||
numWorkers: 1, | ||
maxRetries: 10, | ||
forkOptions: { | ||
env: { | ||
FORCE_COLOR: '1', | ||
...process.env, | ||
}, | ||
}, | ||
exposedMethods: ['initialize'], | ||
}) as any as InstanceType<typeof Worker> & { | ||
initialize: typeof import('./render-server').initialize | ||
} | ||
|
||
let didInitialize = false | ||
|
||
for (const _worker of ((routerWorker as any)._workerPool?._workers || []) as { | ||
_child: ChildProcess | ||
}[]) { | ||
// eslint-disable-next-line no-loop-func | ||
_worker._child.on('exit', (code, signal) => { | ||
// catch failed initializing without retry | ||
if ((code || signal) && !didInitialize) { | ||
routerWorker?.end() | ||
process.exit(1) | ||
} | ||
}) | ||
} | ||
|
||
const workerStdout = routerWorker.getStdout() | ||
const workerStderr = routerWorker.getStderr() | ||
|
||
workerStdout.on('data', (data) => { | ||
process.stdout.write(data) | ||
}) | ||
workerStderr.on('data', (data) => { | ||
process.stderr.write(data) | ||
}) | ||
|
||
const { port: routerPort } = await routerWorker.initialize({ | ||
dir, | ||
port, | ||
dev: false, | ||
hostname, | ||
minimalMode, | ||
workerType: 'router', | ||
}) | ||
didInitialize = true | ||
|
||
const getProxyServer = (pathname: string) => { | ||
const targetUrl = `http://${hostname}:${routerPort}${pathname}` | ||
const proxyServer = httpProxy.createProxy({ | ||
target: targetUrl, | ||
changeOrigin: false, | ||
ignorePath: true, | ||
xfwd: true, | ||
ws: true, | ||
followRedirects: false, | ||
}) | ||
return proxyServer | ||
} | ||
|
||
// proxy to router worker | ||
return async (req: IncomingMessage, res: ServerResponse) => { | ||
const urlParts = (req.url || '').split('?') | ||
const urlNoQuery = urlParts[0] | ||
|
||
// this normalizes repeated slashes in the path e.g. hello//world -> | ||
// hello/world or backslashes to forward slashes, this does not | ||
// handle trailing slash as that is handled the same as a next.config.js | ||
// redirect | ||
if (urlNoQuery?.match(/(\\|\/\/)/)) { | ||
const cleanUrl = normalizeRepeatedSlashes(req.url!) | ||
res.statusCode = 308 | ||
res.setHeader('Location', cleanUrl) | ||
res.end(cleanUrl) | ||
return | ||
} | ||
const proxyServer = getProxyServer(req.url || '/') | ||
proxyServer.web(req, res) | ||
proxyServer.on('error', (err) => { | ||
res.statusCode = 500 | ||
res.end('Internal Server Error') | ||
console.error(err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
83b774e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stats from current release
Default Build (Increase detected⚠️ )
General Overall increase⚠️
Client Bundles (main, webpack) Overall increase⚠️
Legacy Client Bundles (polyfills)
Client Pages Overall decrease ✓
Client Build Manifests
Rendered Page Sizes Overall decrease ✓
Edge SSR bundle Size Overall increase⚠️
Middleware size Overall increase⚠️
Diffs
Diff for page.js
Diff too large to display
Diff for middleware-b..-manifest.js
Diff for middleware-r..-manifest.js
Diff for middleware.js
Diff for edge-ssr.js
Diff too large to display
Diff for _buildManifest.js
Diff for _app-HASH.js
Diff for _error-HASH.js
Diff for amp-HASH.js
Diff for css-HASH.js
Diff for dynamic-HASH.js
Diff for edge-ssr-HASH.js
Diff for head-HASH.js
Diff for hooks-HASH.js
Diff for image-HASH.js
Diff for index-HASH.js
Diff for link-HASH.js
Diff for routerDirect-HASH.js
Diff for script-HASH.js
Diff for withRouter-HASH.js
Diff for 846-HASH.js
Diff too large to display
Diff for 869.HASH.js
Diff for 974ec2d0-HASH.js
Diff too large to display
Diff for page-0f3d25a7c0ecb2fe.js
Diff for page-12bf7c84b5879789.js
Diff for layout-ccf26..8433d050e.js
Diff for main-HASH.js
Diff too large to display
Diff for main-app-HASH.js
Diff for webpack-HASH.js
Diff for index.html
Diff for link.html
Diff for withRouter.html