Unverified Commit b7e26274 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Avoid loading Next.js config again in render workers (#52587)

This PR ensures that both Webpack and the config won't be initiated in render workers. This is great for performance but also avoids potential issues (e.g. Next.js plugin with side effects). Instead, we pass a serialized config from the router worker to the render workers.

Closes #52366.
parent 38dafa16
let installed: boolean = false let installed: boolean = false
export function loadWebpackHook() { export function loadWebpackHook({ init }: { init: boolean }) {
const { init: initWebpack } = require('next/dist/compiled/webpack/webpack') if (init) {
if (installed) { const { init: initWebpack } = require('next/dist/compiled/webpack/webpack')
return if (installed) {
return
}
installed = true
initWebpack()
} }
installed = true
initWebpack()
// hook the Node.js require so that webpack requires are // hook the Node.js require so that webpack requires are
// routed to the bundled and now initialized webpack version // routed to the bundled and now initialized webpack version
......
...@@ -725,8 +725,19 @@ export default async function loadConfig( ...@@ -725,8 +725,19 @@ export default async function loadConfig(
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, curLog) await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, curLog)
if (!process.env.__NEXT_PRIVATE_RENDER_WORKER) { loadWebpackHook({
loadWebpackHook() // For render workers, there's no need to init webpack eagerly
init: !process.env.__NEXT_PRIVATE_RENDER_WORKER,
})
// For the render worker, we directly return the serialized config from the
// parent worker (router worker) to avoid loading it again.
// This is because loading the config might be expensive especiall when people
// have Webpack plugins added.
// Because of this change, unserializable fields like `.webpack` won't be
// existing here but the render worker shouldn't use these as well.
if (process.env.__NEXT_PRIVATE_RENDER_WORKER_CONFIG) {
return JSON.parse(process.env.__NEXT_PRIVATE_RENDER_WORKER_CONFIG)
} }
let configFileName = 'next.config.js' let configFileName = 'next.config.js'
......
import type NextServer from '../../next-server' import type NextServer from '../../next-server'
import type { NextConfigComplete } from '../../config-shared'
import { getNodeOptionsWithoutInspect } from '../utils' import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render' import { deserializeErr, errorToJSON } from '../../render'
...@@ -84,10 +85,11 @@ export const createWorker = async ( ...@@ -84,10 +85,11 @@ export const createWorker = async (
ipcValidationKey: string, ipcValidationKey: string,
isNodeDebugging: boolean | 'brk' | undefined, isNodeDebugging: boolean | 'brk' | undefined,
type: 'pages' | 'app', type: 'pages' | 'app',
useServerActions?: boolean nextConfig: NextConfigComplete
) => { ) => {
const { initialEnv } = require('@next/env') as typeof import('@next/env') const { initialEnv } = require('@next/env') as typeof import('@next/env')
const { Worker } = require('next/dist/compiled/jest-worker') const { Worker } = require('next/dist/compiled/jest-worker')
const useServerActions = !!nextConfig.experimental.serverActions
const worker = new Worker(require.resolve('../render-server'), { const worker = new Worker(require.resolve('../render-server'), {
numWorkers: 1, numWorkers: 1,
...@@ -103,6 +105,7 @@ export const createWorker = async ( ...@@ -103,6 +105,7 @@ export const createWorker = async (
.replace(/--max-old-space-size=[\d]{1,}/, '') .replace(/--max-old-space-size=[\d]{1,}/, '')
.trim(), .trim(),
__NEXT_PRIVATE_RENDER_WORKER: type, __NEXT_PRIVATE_RENDER_WORKER: type,
__NEXT_PRIVATE_RENDER_WORKER_CONFIG: JSON.stringify(nextConfig),
__NEXT_PRIVATE_ROUTER_IPC_PORT: ipcPort + '', __NEXT_PRIVATE_ROUTER_IPC_PORT: ipcPort + '',
__NEXT_PRIVATE_ROUTER_IPC_KEY: ipcValidationKey, __NEXT_PRIVATE_ROUTER_IPC_KEY: ipcValidationKey,
__NEXT_PRIVATE_STANDALONE_CONFIG: __NEXT_PRIVATE_STANDALONE_CONFIG:
......
...@@ -290,14 +290,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -290,14 +290,15 @@ export default class NextNodeServer extends BaseServer {
ipcValidationKey, ipcValidationKey,
options.isNodeDebugging, options.isNodeDebugging,
'app', 'app',
this.nextConfig.experimental.serverActions this.nextConfig
) )
} }
this.renderWorkers.pages = await createWorker( this.renderWorkers.pages = await createWorker(
ipcPort, ipcPort,
ipcValidationKey, ipcValidationKey,
options.isNodeDebugging, options.isNodeDebugging,
'pages' 'pages',
this.nextConfig
) )
this.renderWorkers.middleware = this.renderWorkers.middleware =
this.renderWorkers.pages || this.renderWorkers.app this.renderWorkers.pages || this.renderWorkers.app
......
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return <h1>hello from page</h1>
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app dir - next config',
{
files: __dirname,
},
({ next }) => {
// https://github.com/vercel/next.js/issues/52366
it('should support importing webpack in next.config', async () => {
const html = await next.render('/')
expect(html).toContain('hello from page')
})
}
)
// This should work
console.log(require('webpack').sources.RawSource)
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
return config
},
}
module.exports = nextConfig
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