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