Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/next/src/server/config-utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
let installed: boolean = false

export function loadWebpackHook() {
const { init: initWebpack } = require('next/dist/compiled/webpack/webpack')
if (installed) {
return
export function loadWebpackHook({ init }: { init: boolean }) {
if (init) {
const { init: initWebpack } = require('next/dist/compiled/webpack/webpack')
if (installed) {
return
}
installed = true
initWebpack()
}
installed = true

initWebpack()

// hook the Node.js require so that webpack requires are
// routed to the bundled and now initialized webpack version
Expand Down
15 changes: 13 additions & 2 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,19 @@ export default async function loadConfig(

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'
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type NextServer from '../../next-server'
import type { NextConfigComplete } from '../../config-shared'

import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render'
Expand Down Expand Up @@ -84,10 +85,11 @@ export const createWorker = async (
ipcValidationKey: string,
isNodeDebugging: boolean | 'brk' | undefined,
type: 'pages' | 'app',
useServerActions?: boolean
nextConfig: NextConfigComplete
) => {
const { initialEnv } = require('@next/env') as typeof import('@next/env')
const { Worker } = require('next/dist/compiled/jest-worker')
const useServerActions = !!nextConfig.experimental.serverActions

const worker = new Worker(require.resolve('../render-server'), {
numWorkers: 1,
Expand All @@ -103,6 +105,7 @@ export const createWorker = async (
.replace(/--max-old-space-size=[\d]{1,}/, '')
.trim(),
__NEXT_PRIVATE_RENDER_WORKER: type,
__NEXT_PRIVATE_RENDER_WORKER_CONFIG: JSON.stringify(nextConfig),
__NEXT_PRIVATE_ROUTER_IPC_PORT: ipcPort + '',
__NEXT_PRIVATE_ROUTER_IPC_KEY: ipcValidationKey,
__NEXT_PRIVATE_STANDALONE_CONFIG:
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,15 @@ export default class NextNodeServer extends BaseServer {
ipcValidationKey,
options.isNodeDebugging,
'app',
this.nextConfig.experimental.serverActions
this.nextConfig
)
}
this.renderWorkers.pages = await createWorker(
ipcPort,
ipcValidationKey,
options.isNodeDebugging,
'pages'
'pages',
this.nextConfig
)
this.renderWorkers.middleware =
this.renderWorkers.pages || this.renderWorkers.app
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/next-config/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/next-config/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return <h1>hello from page</h1>
}
15 changes: 15 additions & 0 deletions test/e2e/app-dir/next-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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')
})
}
)
11 changes: 11 additions & 0 deletions test/e2e/app-dir/next-config/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This should work
console.log(require('webpack').sources.RawSource)

/** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
return config
},
}

module.exports = nextConfig