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

fix: allow resolving large JSON data in server components #42025

Merged
merged 13 commits into from Oct 30, 2022
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["debug-react-exp", "dev", "test/e2e/app-dir/app"],
"runtimeArgs": ["debug", "dev", "test/e2e/app-dir/app"],
"skipFiles": ["<node_internals>/**"],
"env": {
"NEXT_PRIVATE_LOCAL_WEBPACK": "1"
Expand Down
45 changes: 29 additions & 16 deletions packages/next/server/config.ts
Expand Up @@ -28,6 +28,11 @@ import { getDependencies } from '../lib/get-package-version'

export { DomainLocale, NextConfig, normalizeConfig } from './config-shared'

const NODE_16_VERSION = '16.8.0'
const NODE_18_VERSION = '18.0.0'
const isAboveNodejs16 = semverGte(process.version, NODE_16_VERSION)
const isAboveNodejs18 = semverGte(process.version, NODE_18_VERSION)

const experimentalWarning = execOnce(
(configFileName: string, features: string[]) => {
const s = features.length > 1 ? 's' : ''
Expand All @@ -52,21 +57,21 @@ const experimentalWarning = execOnce(
}
)

export function setHttpClientAndAgentOptions(options: NextConfig) {
if (semverGte(process.version, '16.8.0')) {
if (
options.experimental?.enableUndici &&
semverGte(process.version, '18.0.0')
) {
export function setHttpClientAndAgentOptions(config: NextConfig) {
if (isAboveNodejs16) {
if (config.experimental?.enableUndici && isAboveNodejs18) {
Log.warn(
'`enableUndici` option is unnecessary in Node.js v18.0.0 or greater.'
`\`enableUndici\` option is unnecessary in Node.js v${NODE_18_VERSION} or greater.`
)
} else {
;(global as any).__NEXT_USE_UNDICI = options.experimental?.enableUndici
// When appDir is enabled undici is the default because of Response.clone() issues in node-fetch
;(global as any).__NEXT_USE_UNDICI = config.experimental?.appDir
? true
: config.experimental?.enableUndici
}
} else if (options.experimental?.enableUndici) {
} else if (config.experimental?.enableUndici) {
Log.warn(
'`enableUndici` option requires Node.js v16.8.0 or greater. Falling back to `node-fetch`'
`\`enableUndici\` option requires Node.js v${NODE_16_VERSION} or greater. Falling back to \`node-fetch\``
)
}
if ((global as any).__NEXT_HTTP_AGENT) {
Expand All @@ -75,13 +80,13 @@ export function setHttpClientAndAgentOptions(options: NextConfig) {
return
}

if (!options) {
if (!config) {
throw new Error('Expected config.httpAgentOptions to be an object')
}

;(global as any).__NEXT_HTTP_AGENT_OPTIONS = options.httpAgentOptions
;(global as any).__NEXT_HTTP_AGENT = new HttpAgent(options.httpAgentOptions)
;(global as any).__NEXT_HTTPS_AGENT = new HttpsAgent(options.httpAgentOptions)
;(global as any).__NEXT_HTTP_AGENT_OPTIONS = config.httpAgentOptions
;(global as any).__NEXT_HTTP_AGENT = new HttpAgent(config.httpAgentOptions)
;(global as any).__NEXT_HTTPS_AGENT = new HttpsAgent(config.httpAgentOptions)
}

async function setFontLoaderDefaults(config: NextConfigComplete, dir: string) {
Expand Down Expand Up @@ -154,6 +159,16 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
for (const featureName of Object.keys(
value
) as (keyof ExperimentalConfig)[]) {
const featureValue = value[featureName]
if (
featureName === 'appDir' &&
featureValue === true &&
!isAboveNodejs16
) {
throw new Error(
`experimental.appDir requires Node v${NODE_16_VERSION} or later.`
)
}
if (
value[featureName] !== defaultConfig.experimental[featureName]
) {
Expand Down Expand Up @@ -608,8 +623,6 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
result.output = undefined
}

// TODO: Change defaultConfig type to NextConfigComplete
// so we don't need "!" here.
setHttpClientAndAgentOptions(result || defaultConfig)

if (result.i18n) {
Expand Down