server: require hook hotfix (#55146)

This PR fixes a bug in the require hook implementation where the variable used might not exist depending on the environment. In order to fix that, we can use the variable used to define the version of React used instead as it is already present and a proxy of the same information.




Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This commit is contained in:
Jimmy Lai 2023-09-08 21:02:17 +02:00 committed by GitHub
parent b5d752667a
commit 6e5b935fd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 15 deletions

View file

@ -1248,7 +1248,6 @@ export default async function build(
forkOptions: {
env: {
...process.env,
__NEXT_PRIVATE_RENDER_RUNTIME: type,
__NEXT_INCREMENTAL_CACHE_IPC_PORT: ipcPort + '',
__NEXT_INCREMENTAL_CACHE_IPC_KEY: ipcValidationKey,
__NEXT_PRIVATE_PREBUNDLED_REACT:
@ -1256,7 +1255,7 @@ export default async function build(
? config.experimental.serverActions
? 'experimental'
: 'next'
: '',
: undefined,
},
},
enableWorkerThreads: config.experimental.workerThreads,

View file

@ -6,6 +6,11 @@ import type {
WorkerUpgradeHandler,
} from './setup-server-worker'
// This is required before other imports to ensure the require hook is setup.
import '../node-polyfill-fetch'
import '../node-environment'
import '../require-hook'
import url from 'url'
import path from 'path'
import loadConfig from '../config'

View file

@ -115,9 +115,12 @@ export const createWorker = async (
__NEXT_PRIVATE_STANDALONE_CONFIG:
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG,
NODE_ENV: process.env.NODE_ENV,
__NEXT_PRIVATE_RENDER_RUNTIME: type,
__NEXT_PRIVATE_PREBUNDLED_REACT:
type === 'app' ? (useServerActions ? 'experimental' : 'next') : '',
type === 'app'
? useServerActions
? 'experimental'
: 'next'
: undefined,
...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.${type}-renderer` }
: {}),

View file

@ -122,19 +122,15 @@ mod._resolveFilename = function (
// that needs to point to the rendering runtime version, it will point to the correct one.
// This can happen on `pages` when a user requires a dependency that uses next/image for example.
// This is only needed in production as in development we fallback to the external version.
if (
process.env.NODE_ENV !== 'development' &&
process.env.__NEXT_PRIVATE_RENDER_RUNTIME &&
!process.env.TURBOPACK
) {
const currentRuntime = `${
process.env.__NEXT_PRIVATE_RENDER_RUNTIME === 'pages'
? 'next/dist/compiled/next-server/pages.runtime'
: 'next/dist/compiled/next-server/app-page.runtime'
}.prod`
if (process.env.NODE_ENV !== 'development' && !process.env.TURBOPACK) {
mod.prototype.require = function (request: string) {
if (request.endsWith('.shared-runtime')) {
const currentRuntime = `${
// this env var is only set in app router
!!process.env.__NEXT_PRIVATE_PREBUNDLED_REACT
? 'next/dist/compiled/next-server/app-page.runtime'
: 'next/dist/compiled/next-server/pages.runtime'
}.prod`
const base = path.basename(request, '.shared-runtime')
const camelized = base.replace(/-([a-z])/g, (g) => g[1].toUpperCase())
const instance = originalRequire.call(this, currentRuntime)