Unverified Commit e6acd40c authored by Steven's avatar Steven Committed by GitHub

feat: make `enableUndici: true` default and remove `node-fetch` (#49061)

- Closes #49053
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent a40fa12c
......@@ -2,4 +2,3 @@ import 'next/dist/server/node-polyfill-fetch'
import 'next/dist/server/node-polyfill-web-streams'
import 'next/dist/server/node-polyfill-headers'
import './async-local-storage'
;(globalThis as any).__NEXT_USE_UNDICI = true
......@@ -1224,7 +1224,6 @@ export default async function build(
configFileName,
runtimeEnvConfig,
httpAgentOptions: config.httpAgentOptions,
enableUndici: config.experimental.enableUndici,
locales: config.i18n?.locales,
defaultLocale: config.i18n?.defaultLocale,
nextConfigOutput: config.output,
......@@ -1408,7 +1407,6 @@ export default async function build(
configFileName,
runtimeEnvConfig,
httpAgentOptions: config.httpAgentOptions,
enableUndici: config.experimental.enableUndici,
locales: config.i18n?.locales,
defaultLocale: config.i18n?.defaultLocale,
parentId: isPageStaticSpan.id,
......
......@@ -1324,7 +1324,6 @@ export async function isPageStatic({
configFileName,
runtimeEnvConfig,
httpAgentOptions,
enableUndici,
locales,
defaultLocale,
parentId,
......@@ -1342,7 +1341,6 @@ export async function isPageStatic({
configFileName: string
runtimeEnvConfig: any
httpAgentOptions: NextConfigComplete['httpAgentOptions']
enableUndici?: NextConfigComplete['experimental']['enableUndici']
locales?: string[]
defaultLocale?: string
parentId?: any
......@@ -1375,7 +1373,6 @@ export async function isPageStatic({
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
setHttpClientAndAgentOptions({
httpAgentOptions,
experimental: { enableUndici },
})
let componentsResult: LoadComponentsReturnType
......
......@@ -4,7 +4,9 @@ import childProcess from 'child_process'
import chalk from 'next/dist/compiled/chalk'
import arg from 'next/dist/compiled/arg/index.js'
import fetch from 'next/dist/compiled/node-fetch'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
import { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands'
import isError from '../lib/is-error'
......
......@@ -715,7 +715,6 @@ export default async function exportApp(
parentSpanId: pageExportSpan.id,
httpAgentOptions: nextConfig.httpAgentOptions,
serverComponents: options.hasAppDir,
enableUndici: nextConfig.experimental.enableUndici,
debugOutput: options.debugOutput,
isrMemoryCacheSize: nextConfig.experimental.isrMemoryCacheSize,
fetchCache: nextConfig.experimental.appDir,
......
......@@ -79,7 +79,6 @@ interface ExportPageInput {
parentSpanId: any
httpAgentOptions: NextConfigComplete['httpAgentOptions']
serverComponents?: boolean
enableUndici: NextConfigComplete['experimental']['enableUndici']
debugOutput?: boolean
isrMemoryCacheSize?: NextConfigComplete['experimental']['isrMemoryCacheSize']
fetchCache?: boolean
......@@ -136,7 +135,6 @@ export default async function exportPage({
disableOptimizedLoading,
httpAgentOptions,
serverComponents,
enableUndici,
debugOutput,
isrMemoryCacheSize,
fetchCache,
......@@ -145,7 +143,6 @@ export default async function exportPage({
}: ExportPageInput): Promise<ExportPageResults> {
setHttpClientAndAgentOptions({
httpAgentOptions,
experimental: { enableUndici },
})
const exportPageSpan = trace('export-page-worker', parentSpanId)
......
......@@ -3,7 +3,12 @@ import fs from 'fs'
import path from 'path'
import * as Log from '../build/output/log'
import tar from 'next/dist/compiled/tar'
import fetch from 'next/dist/compiled/node-fetch'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
const { WritableStream } = require('node:stream/web') as {
WritableStream: typeof global.WritableStream
}
import { fileExists } from './file-exists'
import { getRegistry } from './helpers/get-registry'
......@@ -97,17 +102,24 @@ export async function downloadWasmSwc(
const registry = getRegistry()
await fetch(`${registry}${pkgName}/-/${tarFileName}`).then((res) => {
if (!res.ok) {
const { ok, body } = res
if (!ok) {
throw new Error(`request failed with status ${res.status}`)
}
if (!body) {
throw new Error('request failed with empty body')
}
const cacheWriteStream = fs.createWriteStream(tempFile)
return new Promise<void>((resolve, reject) => {
res.body
.pipe(cacheWriteStream)
.on('error', (err) => reject(err))
.on('finish', () => resolve())
}).finally(() => cacheWriteStream.close())
return body.pipeTo(
new WritableStream({
write(chunk) {
cacheWriteStream.write(chunk)
},
close() {
cacheWriteStream.close()
},
})
)
})
await fs.promises.rename(tempFile, path.join(cacheDirectory, tarFileName))
}
......
......@@ -417,9 +417,6 @@ const configSchema = {
},
type: 'array',
},
enableUndici: {
type: 'boolean',
},
workerThreads: {
type: 'boolean',
},
......
......@@ -215,7 +215,6 @@ export interface ExperimentalConfig {
* [webpack/webpack#ModuleNotoundError.js#L13-L42](https://github.com/webpack/webpack/blob/2a0536cf510768111a3a6dceeb14cb79b9f59273/lib/ModuleNotFoundError.js#L13-L42)
*/
fallbackNodePolyfills?: false
enableUndici?: boolean
sri?: {
algorithm?: SubresourceIntegrityAlgorithm
}
......@@ -700,7 +699,6 @@ export const defaultConfig: NextConfig = {
disablePostcssPresetEnv: undefined,
amp: undefined,
urlImports: undefined,
enableUndici: false,
adjustFontFallbacks: false,
adjustFontFallbacksWithSizeAdjust: false,
turbo: undefined,
......
......@@ -27,16 +27,10 @@ import {
import { loadWebpackHook } from './config-utils'
import { ImageConfig, imageConfigDefault } from '../shared/lib/image-config'
import { loadEnvConfig } from '@next/env'
import { gte as semverGte } from 'next/dist/compiled/semver'
import { flushAndExit } from '../telemetry/flush-and-exit'
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' : ''
......@@ -61,26 +55,9 @@ const experimentalWarning = execOnce(
}
)
export function setHttpClientAndAgentOptions(
config: {
httpAgentOptions?: NextConfig['httpAgentOptions']
experimental?: {
enableUndici?: boolean
}
},
silent = false
) {
if (isAboveNodejs16) {
// Node.js 18 has undici built-in.
if (config.experimental?.enableUndici && !isAboveNodejs18) {
// When appDir is enabled undici is the default because of Response.clone() issues in node-fetch
;(globalThis as any).__NEXT_USE_UNDICI = config.experimental?.enableUndici
}
} else if (config.experimental?.enableUndici && !silent) {
Log.warn(
`\`enableUndici\` option requires Node.js v${NODE_16_VERSION} or greater. Falling back to \`node-fetch\``
)
}
export function setHttpClientAndAgentOptions(config: {
httpAgentOptions?: NextConfig['httpAgentOptions']
}) {
if ((globalThis as any).__NEXT_HTTP_AGENT) {
// We only need to assign once because we want
// to reuse the same agent for all requests.
......@@ -165,11 +142,6 @@ function assignDefaults(
) as (keyof ExperimentalConfig)[]) {
const featureValue = value[featureName]
if (featureName === 'appDir' && featureValue === true) {
if (!isAboveNodejs16) {
throw new Error(
`experimental.appDir requires Node v${NODE_16_VERSION} or later.`
)
}
// auto enable clientRouterFilter if not manually set
// when appDir is enabled
if (
......@@ -314,10 +286,6 @@ function assignDefaults(
)
}
if (result.experimental?.appDir) {
result.experimental.enableUndici = true
}
if (result.basePath !== '') {
if (result.basePath === '/') {
throw new Error(
......@@ -538,7 +506,7 @@ function assignDefaults(
result.output = undefined
}
setHttpClientAndAgentOptions(result || defaultConfig, silent)
setHttpClientAndAgentOptions(result || defaultConfig)
if (result.i18n) {
const { i18n } = result
......@@ -877,6 +845,6 @@ export default async function loadConfig(
silent
) as NextConfigComplete
completeConfig.configFileName = configFileName
setHttpClientAndAgentOptions(completeConfig, silent)
setHttpClientAndAgentOptions(completeConfig)
return completeConfig
}
......@@ -1643,7 +1643,6 @@ export default class DevServer extends Server {
publicRuntimeConfig,
serverRuntimeConfig,
httpAgentOptions,
experimental: { enableUndici },
} = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.i18n || {}
const staticPathsWorker = this.getStaticPathsWorker()
......@@ -1658,7 +1657,6 @@ export default class DevServer extends Server {
serverRuntimeConfig,
},
httpAgentOptions,
enableUndici,
locales,
defaultLocale,
originalAppPath,
......
......@@ -26,7 +26,6 @@ export async function loadStaticPaths({
pathname,
config,
httpAgentOptions,
enableUndici,
locales,
defaultLocale,
isAppPath,
......@@ -41,7 +40,6 @@ export async function loadStaticPaths({
pathname: string
config: RuntimeConfig
httpAgentOptions: NextConfigComplete['httpAgentOptions']
enableUndici: NextConfigComplete['enableUndici']
locales?: string[]
defaultLocale?: string
isAppPath?: boolean
......@@ -60,7 +58,6 @@ export async function loadStaticPaths({
require('../../shared/lib/runtime-config').setConfig(config)
setHttpClientAndAgentOptions({
httpAgentOptions,
experimental: { enableUndici },
})
const components = await loadComponents({
......
......@@ -3,9 +3,7 @@
if (!(global as any).fetch) {
function getFetchImpl() {
return (global as any).__NEXT_USE_UNDICI
? require('next/dist/compiled/undici')
: require('next/dist/compiled/node-fetch')
return require('next/dist/compiled/undici')
}
function getRequestImpl() {
......@@ -22,30 +20,19 @@ if (!(global as any).fetch) {
;(global as any).fetch = (...args: any[]) => {
const fetchImpl = getFetchImpl()
if ((global as any).__NEXT_USE_UNDICI) {
// Undici does not support the `keepAlive` option,
// instead we have to pass a custom dispatcher
if (
!(global as any).__NEXT_HTTP_AGENT_OPTIONS?.keepAlive &&
!(global as any).__NEXT_UNDICI_AGENT_SET
) {
;(global as any).__NEXT_UNDICI_AGENT_SET = true
fetchImpl.setGlobalDispatcher(new fetchImpl.Agent({ pipelining: 0 }))
}
return fetchImpl.fetch(...args)
}
const agent = ({ protocol }: any) =>
protocol === 'http:'
? (global as any).__NEXT_HTTP_AGENT
: (global as any).__NEXT_HTTPS_AGENT
if (!args[1]) {
args[1] = { agent }
} else if (!args[1].agent) {
args[1].agent = agent
// Undici does not support the `keepAlive` option,
// instead we have to pass a custom dispatcher
if (
!(global as any).__NEXT_HTTP_AGENT_OPTIONS?.keepAlive &&
!(global as any).__NEXT_UNDICI_AGENT_SET
) {
;(global as any).__NEXT_UNDICI_AGENT_SET = true
fetchImpl.setGlobalDispatcher(new fetchImpl.Agent({ pipelining: 0 }))
console.warn(
'Warning - Configuring `keepAlive: false` is deprecated. Use `{ headers: { connection: "close" } }` instead.'
)
}
return fetchImpl(...args)
return fetchImpl.fetch(...args)
}
Object.defineProperties(global, {
......
import retry from 'next/dist/compiled/async-retry'
import fetch from 'next/dist/compiled/node-fetch'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
export function _postPayload(endpoint: string, body: object, signal?: any) {
if (!signal && 'timeout' in AbortSignal) {
// @ts-expect-error Needs @types/node@16.14.0 or newer
signal = AbortSignal.timeout(5000)
}
return (
retry(
() =>
......@@ -9,7 +15,6 @@ export function _postPayload(endpoint: string, body: object, signal?: any) {
method: 'POST',
body: JSON.stringify(body),
headers: { 'content-type': 'application/json' },
timeout: 5000,
signal,
}).then((res) => {
if (!res.ok) {
......
......@@ -70,8 +70,8 @@ module.exports = function (task) {
},
env: {
targets: {
// Should match version defined in packages/next/package.json#engine
// Should also match NODE_16_VERSION in packages/next/src/server/config.ts
// Follows version defined in packages/next/package.json#engine
// Currently a few minors behind due to babel class transpiling
node: '16.8.0',
},
},
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import semver from 'semver'
if (
semver.lt(process.version, '16.8.0') ||
semver.gte(process.version, '18.0.0') ||
(global as any).isNextDeploy
) {
it('skipping for Node.js versions <16.8.0 and >18.0.0', () => {
expect(true).toBe(true)
})
} else {
describe('undici fetch', () => {
let next: NextInstance
describe('undici fetch', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/api/globalFetch.js': `
import { ReadableStream } from 'node:stream/web';
export default async function globalFetch(req, res) {
try {
const response = await fetch('https://example.vercel.sh')
res.json({ value: response.body instanceof ReadableStream })
} catch (error) {
console.error(error);
res.send(error);
}
}
`,
'pages/api/globalHeaders.js': `
export default async function globalHeaders(req, res) {
res.json({
value: (new Headers())[Symbol.iterator].name === 'entries'
})
}
`,
'pages/api/globalRequest.js': `
export default async function globalRequest(req, res) {
res.json({
value: (new Request('https://example.vercel.sh')).headers[Symbol.iterator].name === 'entries'
})
beforeAll(async () => {
next = await createNext({
files: {
'pages/api/globalFetch.js': `
import { ReadableStream } from 'node:stream/web';
export default async function globalFetch(req, res) {
try {
const response = await fetch('https://example.vercel.sh')
res.json({ value: response.body instanceof ReadableStream })
} catch (error) {
console.error(error);
res.send(error);
}
`,
'pages/api/globalResponse.js': `
export default async function globalResponse(req, res) {
res.json({
value: (new Response()).headers[Symbol.iterator].name === 'entries'
})
}
`,
},
dependencies: {},
nextConfig: {
experimental: {
enableUndici: true,
},
},
})
}
`,
'pages/api/globalHeaders.js': `
export default async function globalHeaders(req, res) {
res.json({
value: (new Headers())[Symbol.iterator].name === 'entries'
})
}
`,
'pages/api/globalRequest.js': `
export default async function globalRequest(req, res) {
res.json({
value: (new Request('https://example.vercel.sh')).headers[Symbol.iterator].name === 'entries'
})
}
`,
'pages/api/globalResponse.js': `
export default async function globalResponse(req, res) {
res.json({
value: (new Response()).headers[Symbol.iterator].name === 'entries'
})
}
`,
},
dependencies: {},
})
afterAll(() => next.destroy())
describe('undici', () => {
it('global fetch should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalFetch')
const data = await result.json()
expect(data.value).toBe(true)
})
it('global Headers should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalHeaders')
const data = await result.json()
expect(data.value).toBe(true)
})
it('global Request should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalRequest')
const data = await result.json()
expect(data.value).toBe(true)
})
})
afterAll(() => next.destroy())
it('global Response should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalResponse')
const data = await result.json()
expect(data.value).toBe(true)
})
describe('undici', () => {
it('global fetch should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalFetch')
const data = await result.json()
expect(data.value).toBe(true)
})
describe('node-fetch', () => {
beforeAll(async () => {
await next.stop()
await next.patchFile(
'next.config.js',
`module.exports = ${JSON.stringify({
experimental: {
enableUndici: false,
},
})}`
)
await next.start()
})
it('global fetch should return false when node-fetch is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalFetch')
const data = await result.json()
expect(data.value).toBe(false)
})
it('global Headers should return false when node-fetch is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalHeaders')
const data = await result.json()
expect(data.value).toBe(false)
})
it('global Headers should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalHeaders')
const data = await result.json()
expect(data.value).toBe(true)
})
it('global Request should return false when node-fetch is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalRequest')
const data = await result.json()
expect(data.value).toBe(false)
})
it('global Request should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalRequest')
const data = await result.json()
expect(data.value).toBe(true)
})
it('global Response should return false when node-fetch is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalResponse')
const data = await result.json()
expect(data.value).toBe(false)
})
it('global Response should return true when undici is used', async () => {
const result = await fetchViaHTTP(next.url, '/api/globalResponse')
const data = await result.json()
expect(data.value).toBe(true)
})
})
}
})
......@@ -84,14 +84,14 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = {
experimental: {
enableUndici: true,
urlImports: true,
workerThreads: true,
}
}
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
'You have enabled experimental features (enableUndici, workerThreads) in next.config.js.'
'You have enabled experimental features (urlImports, workerThreads) in next.config.js.'
)
})
......
module.exports = {
experimental: {
enableUndici: true,
},
}
export default async function handler(_req, res) {
const fetchRes = await fetch('http://localhost:44001')
const props = await fetchRes.json()
res.json(props)
}
export default function Blog(props) {
return <pre id="props">{JSON.stringify(props)}</pre>
}
export async function getStaticProps({ params: { slug } }) {
return { props: { slug } }
}
export async function getStaticPaths() {
const res = await fetch('http://localhost:44001')
const obj = await res.json()
if (obj.connection === 'keep-alive') {
return {
paths: [{ params: { slug: 'first' } }],
fallback: false,
}
}
return {
paths: [],
fallback: false,
}
}
export default function SSG(props) {
return <pre id="props">{JSON.stringify(props)}</pre>
}
export async function getStaticProps() {
const res = await fetch('http://localhost:44001')
const props = await res.json()
return { props }
}
export default function SSR(props) {
return <pre id="props">{JSON.stringify(props)}</pre>
}
export async function getServerSideProps() {
const res = await fetch('http://localhost:44001')
const props = await res.json()
return { props }
}
/* eslint-env jest */
import { join } from 'path'
import { createServer } from 'http'
import {
fetchViaHTTP,
nextBuild,
findPort,
nextStart,
launchApp,
killApp,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../')
let appPort
let app
let mockServer
describe('undici-keep-alive', () => {
describe('dev', () => {
beforeAll(async () => {
mockServer = createServer((req, res) => {
// we can test request headers by sending them
// back with the response
const { connection } = req.headers
res.end(JSON.stringify({ connection }))
})
mockServer.listen(44001)
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
mockServer.close()
})
runTests()
})
describe('production', () => {
beforeAll(async () => {
mockServer = createServer((req, res) => {
// we can test request headers by sending them
// back with the response
const { connection } = req.headers
res.end(JSON.stringify({ connection }))
})
mockServer.listen(44001)
const { stdout, stderr } = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
})
if (stdout) console.log(stdout)
if (stderr) console.error(stderr)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
mockServer.close()
})
runTests()
})
function runTests() {
it('should send keep-alive for json API', async () => {
const res = await fetchViaHTTP(appPort, '/api/json')
const obj = await res.json()
expect(obj).toEqual({ connection: 'keep-alive' })
})
it('should send keep-alive for getStaticProps', async () => {
const browser = await webdriver(appPort, '/ssg')
const props = await browser.elementById('props').text()
const obj = JSON.parse(props)
expect(obj).toEqual({ connection: 'keep-alive' })
await browser.close()
})
it('should send keep-alive for getStaticPaths', async () => {
const browser = await webdriver(appPort, '/blog/first')
const props = await browser.elementById('props').text()
const obj = JSON.parse(props)
expect(obj).toEqual({ slug: 'first' })
await browser.close()
})
it('should send keep-alive for getServerSideProps', async () => {
const browser = await webdriver(appPort, '/ssr')
const props = await browser.elementById('props').text()
const obj = JSON.parse(props)
expect(obj).toEqual({ connection: 'keep-alive' })
await browser.close()
})
}
})
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