Unverified Commit 2e6588d0 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

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

Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 6d53e8b3
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"runtimeExecutable": "pnpm", "runtimeExecutable": "pnpm",
"runtimeArgs": ["debug-react-exp", "dev", "test/e2e/app-dir/app"], "runtimeArgs": ["debug", "dev", "test/e2e/app-dir/app"],
"skipFiles": ["<node_internals>/**"], "skipFiles": ["<node_internals>/**"],
"env": { "env": {
"NEXT_PRIVATE_LOCAL_WEBPACK": "1" "NEXT_PRIVATE_LOCAL_WEBPACK": "1"
......
...@@ -28,6 +28,11 @@ import { getDependencies } from '../lib/get-package-version' ...@@ -28,6 +28,11 @@ import { getDependencies } from '../lib/get-package-version'
export { DomainLocale, NextConfig, normalizeConfig } from './config-shared' 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( const experimentalWarning = execOnce(
(configFileName: string, features: string[]) => { (configFileName: string, features: string[]) => {
const s = features.length > 1 ? 's' : '' const s = features.length > 1 ? 's' : ''
...@@ -52,21 +57,21 @@ const experimentalWarning = execOnce( ...@@ -52,21 +57,21 @@ const experimentalWarning = execOnce(
} }
) )
export function setHttpClientAndAgentOptions(options: NextConfig) { export function setHttpClientAndAgentOptions(config: NextConfig) {
if (semverGte(process.version, '16.8.0')) { if (isAboveNodejs16) {
if ( if (config.experimental?.enableUndici && isAboveNodejs18) {
options.experimental?.enableUndici &&
semverGte(process.version, '18.0.0')
) {
Log.warn( 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 { } 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( 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) { if ((global as any).__NEXT_HTTP_AGENT) {
...@@ -75,13 +80,13 @@ export function setHttpClientAndAgentOptions(options: NextConfig) { ...@@ -75,13 +80,13 @@ export function setHttpClientAndAgentOptions(options: NextConfig) {
return return
} }
if (!options) { if (!config) {
throw new Error('Expected config.httpAgentOptions to be an object') 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_OPTIONS = config.httpAgentOptions
;(global as any).__NEXT_HTTP_AGENT = new HttpAgent(options.httpAgentOptions) ;(global as any).__NEXT_HTTP_AGENT = new HttpAgent(config.httpAgentOptions)
;(global as any).__NEXT_HTTPS_AGENT = new HttpsAgent(options.httpAgentOptions) ;(global as any).__NEXT_HTTPS_AGENT = new HttpsAgent(config.httpAgentOptions)
} }
async function setFontLoaderDefaults(config: NextConfigComplete, dir: string) { async function setFontLoaderDefaults(config: NextConfigComplete, dir: string) {
...@@ -154,6 +159,16 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) { ...@@ -154,6 +159,16 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
for (const featureName of Object.keys( for (const featureName of Object.keys(
value value
) as (keyof ExperimentalConfig)[]) { ) 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 ( if (
value[featureName] !== defaultConfig.experimental[featureName] value[featureName] !== defaultConfig.experimental[featureName]
) { ) {
...@@ -608,8 +623,6 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) { ...@@ -608,8 +623,6 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
result.output = undefined result.output = undefined
} }
// TODO: Change defaultConfig type to NextConfigComplete
// so we don't need "!" here.
setHttpClientAndAgentOptions(result || defaultConfig) setHttpClientAndAgentOptions(result || defaultConfig)
if (result.i18n) { if (result.i18n) {
......
This diff is collapsed.
export default async function Home() {
const res = await fetch(
`http://localhost:${process.env.API_SERVER_PORT || 3000}/api/large-data`
)
const resClone = res.clone()
const json = await resClone.json()
const firstItem = json.arrayOfObjects[0]
return (
<>
<h1 id="done">Hello world</h1>
<p id="index">{firstItem.index}</p>
<p id="random">{firstItem.random}</p>
</>
)
}
...@@ -4,8 +4,11 @@ import { NextInstance } from 'test/lib/next-modes/base' ...@@ -4,8 +4,11 @@ import { NextInstance } from 'test/lib/next-modes/base'
import { import {
check, check,
fetchViaHTTP, fetchViaHTTP,
findPort,
getRedboxHeader, getRedboxHeader,
hasRedbox, hasRedbox,
initNextServerScript,
killApp,
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
...@@ -16,9 +19,13 @@ import webdriver from 'next-webdriver' ...@@ -16,9 +19,13 @@ import webdriver from 'next-webdriver'
describe('app dir', () => { describe('app dir', () => {
const isDev = (global as any).isNextDev const isDev = (global as any).isNextDev
let next: NextInstance let next: NextInstance
let apiServerPort: number
let apiInstance: any
function runTests() { function runTests() {
beforeAll(async () => { beforeAll(async () => {
apiServerPort = await findPort()
next = await createNext({ next = await createNext({
files: new FileRef(path.join(__dirname, 'app')), files: new FileRef(path.join(__dirname, 'app')),
dependencies: { dependencies: {
...@@ -27,12 +34,26 @@ describe('app dir', () => { ...@@ -27,12 +34,26 @@ describe('app dir', () => {
'react-dom': 'latest', 'react-dom': 'latest',
sass: 'latest', sass: 'latest',
}, },
env: {
API_SERVER_PORT: apiServerPort.toString(),
},
skipStart: true, skipStart: true,
}) })
apiInstance = await initNextServerScript(
path.join(__dirname, 'app', 'api-server.js'),
/Listening on/,
{ ...process.env, PORT: apiServerPort },
undefined,
{ cwd: next.testDir }
)
await next.start() await next.start()
}) })
afterAll(() => next.destroy()) afterAll(() => {
next.destroy()
killApp(apiInstance)
})
if (!(global as any).isNextDeploy) { if (!(global as any).isNextDeploy) {
it('should not share edge workers', async () => { it('should not share edge workers', async () => {
...@@ -2198,6 +2219,21 @@ describe('app dir', () => { ...@@ -2198,6 +2219,21 @@ describe('app dir', () => {
) )
}) })
}) })
describe('data fetch with response over 16KB with chunked encoding', () => {
it('should load page when fetching a large amount of data', async () => {
const browser = await webdriver(next.url, '/very-large-data-fetch')
expect(
await (await browser.waitForElementByCss('#done', 5000)).text()
).toBe('Hello world')
expect(
await (await browser.waitForElementByCss('#index', 2000)).text()
).toBe('0')
expect(
await (await browser.waitForElementByCss('#random', 2000)).text()
).toBe('5')
})
})
} }
runTests() runTests()
......
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