Unverified Commit 46e7658f authored by Steven's avatar Steven Committed by GitHub

Add `next.config.js` option to override default `keepAlive` (#27709)

Follow up to #27376 so users can disable keep-alive.

See comment https://github.com/vercel/next.js/pull/27376#issuecomment-885415623
parent b36c66a4
---
description: Next.js will automatically use HTTP Keep-Alive by default. Learn more about how to disable HTTP Keep-Alive here.
---
# Disabling HTTP Keep-Alive
Next.js automatically polyfills [node-fetch](/docs/basic-features/supported-browsers-features#polyfills) and enables [HTTP Keep-Alive](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive) by default. You may want to disable HTTP Keep-Alive for certain `fetch()` calls or globally.
For a single `fetch()` call, you can add the agent option:
```js
import { Agent } from 'https'
const url = 'https://example.com'
const agent = new Agent({ keepAlive: false })
fetch(url, { agent })
```
To override all `fetch()` calls globally, you can use `next.config.js`:
```js
module.exports = {
httpAgentOptions: {
keepAlive: false,
},
}
```
## Related
<div class="card">
<a href="/docs/api-reference/next.config.js/introduction.md">
<b>Introduction to next.config.js:</b>
<small>Learn more about the configuration file used by Next.js.</small>
</a>
</div>
...@@ -348,6 +348,10 @@ ...@@ -348,6 +348,10 @@
"title": "Disabling ETag Generation", "title": "Disabling ETag Generation",
"path": "/docs/api-reference/next.config.js/disabling-etag-generation.md" "path": "/docs/api-reference/next.config.js/disabling-etag-generation.md"
}, },
{
"title": "Disabling HTTP Keep-Alive",
"path": "/docs/api-reference/next.config.js/disabling-http-keep-alive.md"
},
{ {
"title": "Setting a custom build directory", "title": "Setting a custom build directory",
"path": "/docs/api-reference/next.config.js/setting-a-custom-build-directory.md" "path": "/docs/api-reference/next.config.js/setting-a-custom-build-directory.md"
......
...@@ -743,6 +743,7 @@ export default async function build( ...@@ -743,6 +743,7 @@ export default async function build(
distDir, distDir,
isLikeServerless, isLikeServerless,
runtimeEnvConfig, runtimeEnvConfig,
config.httpAgentOptions,
config.i18n?.locales, config.i18n?.locales,
config.i18n?.defaultLocale config.i18n?.defaultLocale
) )
...@@ -812,6 +813,7 @@ export default async function build( ...@@ -812,6 +813,7 @@ export default async function build(
distDir, distDir,
isLikeServerless, isLikeServerless,
runtimeEnvConfig, runtimeEnvConfig,
config.httpAgentOptions,
config.i18n?.locales, config.i18n?.locales,
config.i18n?.defaultLocale, config.i18n?.defaultLocale,
isPageStaticSpan.id isPageStaticSpan.id
......
...@@ -32,6 +32,8 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' ...@@ -32,6 +32,8 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import * as Log from './output/log' import * as Log from './output/log'
import { loadComponents } from '../server/load-components' import { loadComponents } from '../server/load-components'
import { trace } from '../telemetry/trace' import { trace } from '../telemetry/trace'
import { setHttpAgentOptions } from '../server/config'
import { NextConfigComplete } from '../server/config-shared'
const fileGzipStats: { [k: string]: Promise<number> | undefined } = {} const fileGzipStats: { [k: string]: Promise<number> | undefined } = {}
const fsStatGzip = (file: string) => { const fsStatGzip = (file: string) => {
...@@ -815,6 +817,7 @@ export async function isPageStatic( ...@@ -815,6 +817,7 @@ export async function isPageStatic(
distDir: string, distDir: string,
serverless: boolean, serverless: boolean,
runtimeEnvConfig: any, runtimeEnvConfig: any,
httpAgentOptions: NextConfigComplete['httpAgentOptions'],
locales?: string[], locales?: string[],
defaultLocale?: string, defaultLocale?: string,
parentId?: any parentId?: any
...@@ -833,6 +836,7 @@ export async function isPageStatic( ...@@ -833,6 +836,7 @@ export async function isPageStatic(
return isPageStaticSpan.traceAsyncFn(async () => { return isPageStaticSpan.traceAsyncFn(async () => {
try { try {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig) require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
setHttpAgentOptions(httpAgentOptions)
const components = await loadComponents(distDir, page, serverless) const components = await loadComponents(distDir, page, serverless)
const mod = components.ComponentMod const mod = components.ComponentMod
const Comp = mod.default || mod const Comp = mod.default || mod
......
...@@ -570,6 +570,7 @@ export default async function exportApp( ...@@ -570,6 +570,7 @@ export default async function exportApp(
disableOptimizedLoading: disableOptimizedLoading:
nextConfig.experimental.disableOptimizedLoading, nextConfig.experimental.disableOptimizedLoading,
parentSpanId: pageExportSpan.id, parentSpanId: pageExportSpan.id,
httpAgentOptions: nextConfig.httpAgentOptions,
}) })
for (const validation of result.ampValidations || []) { for (const validation of result.ampValidations || []) {
......
...@@ -19,6 +19,8 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' ...@@ -19,6 +19,8 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../telemetry/trace' import { trace } from '../telemetry/trace'
import { isInAmpMode } from '../shared/lib/amp' import { isInAmpMode } from '../shared/lib/amp'
import { resultFromChunks, resultToChunks } from '../server/utils' import { resultFromChunks, resultToChunks } from '../server/utils'
import { NextConfigComplete } from '../server/config-shared'
import { setHttpAgentOptions } from '../server/config'
const envConfig = require('../shared/lib/runtime-config') const envConfig = require('../shared/lib/runtime-config')
...@@ -55,6 +57,7 @@ interface ExportPageInput { ...@@ -55,6 +57,7 @@ interface ExportPageInput {
optimizeCss: any optimizeCss: any
disableOptimizedLoading: any disableOptimizedLoading: any
parentSpanId: any parentSpanId: any
httpAgentOptions: NextConfigComplete['httpAgentOptions']
} }
interface ExportPageResults { interface ExportPageResults {
...@@ -103,7 +106,9 @@ export default async function exportPage({ ...@@ -103,7 +106,9 @@ export default async function exportPage({
optimizeImages, optimizeImages,
optimizeCss, optimizeCss,
disableOptimizedLoading, disableOptimizedLoading,
httpAgentOptions,
}: ExportPageInput): Promise<ExportPageResults> { }: ExportPageInput): Promise<ExportPageResults> {
setHttpAgentOptions(httpAgentOptions)
const exportPageSpan = trace('export-page-worker', parentSpanId) const exportPageSpan = trace('export-page-worker', parentSpanId)
return exportPageSpan.traceAsyncFn(async () => { return exportPageSpan.traceAsyncFn(async () => {
......
...@@ -76,7 +76,7 @@ export type NextConfig = { [key: string]: any } & { ...@@ -76,7 +76,7 @@ export type NextConfig = { [key: string]: any } & {
reactStrictMode?: boolean reactStrictMode?: boolean
publicRuntimeConfig?: { [key: string]: any } publicRuntimeConfig?: { [key: string]: any }
serverRuntimeConfig?: { [key: string]: any } serverRuntimeConfig?: { [key: string]: any }
httpAgentOptions?: { keepAlive?: boolean }
future?: { future?: {
/** /**
* @deprecated this options was moved to the top level * @deprecated this options was moved to the top level
...@@ -153,6 +153,9 @@ export const defaultConfig: NextConfig = { ...@@ -153,6 +153,9 @@ export const defaultConfig: NextConfig = {
serverRuntimeConfig: {}, serverRuntimeConfig: {},
publicRuntimeConfig: {}, publicRuntimeConfig: {},
reactStrictMode: false, reactStrictMode: false,
httpAgentOptions: {
keepAlive: true,
},
experimental: { experimental: {
cpus: Math.max( cpus: Math.max(
1, 1,
......
import chalk from 'chalk' import chalk from 'chalk'
import findUp from 'next/dist/compiled/find-up' import findUp from 'next/dist/compiled/find-up'
import { basename, extname } from 'path' import { basename, extname } from 'path'
import { Agent as HttpAgent } from 'http'
import { Agent as HttpsAgent } from 'https'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants' import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
...@@ -304,6 +306,12 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -304,6 +306,12 @@ function assignDefaults(userConfig: { [key: string]: any }) {
} }
} }
// TODO: Change defaultConfig type to NextConfigComplete
// so we don't need "!" here.
setHttpAgentOptions(
result.httpAgentOptions || defaultConfig.httpAgentOptions!
)
if (result.i18n) { if (result.i18n) {
const { i18n } = result const { i18n } = result
const i18nType = typeof i18n const i18nType = typeof i18n
...@@ -517,7 +525,9 @@ export default async function loadConfig( ...@@ -517,7 +525,9 @@ export default async function loadConfig(
} }
} }
return defaultConfig as NextConfigComplete const completeConfig = defaultConfig as NextConfigComplete
setHttpAgentOptions(completeConfig.httpAgentOptions)
return completeConfig
} }
export function isTargetLikeServerless(target: string) { export function isTargetLikeServerless(target: string) {
...@@ -525,3 +535,20 @@ export function isTargetLikeServerless(target: string) { ...@@ -525,3 +535,20 @@ export function isTargetLikeServerless(target: string) {
const isServerlessTrace = target === 'experimental-serverless-trace' const isServerlessTrace = target === 'experimental-serverless-trace'
return isServerless || isServerlessTrace return isServerless || isServerlessTrace
} }
export function setHttpAgentOptions(
options: NextConfigComplete['httpAgentOptions']
) {
if ((global as any).__NEXT_HTTP_AGENT) {
// We only need to assign once because we want
// to resuse the same agent for all requests.
return
}
if (!options) {
throw new Error('Expected config.httpAgentOptions to be an object')
}
;(global as any).__NEXT_HTTP_AGENT = new HttpAgent(options)
;(global as any).__NEXT_HTTPS_AGENT = new HttpsAgent(options)
}
...@@ -566,7 +566,11 @@ export default class DevServer extends Server { ...@@ -566,7 +566,11 @@ export default class DevServer extends Server {
// from waiting on them for the page to load in dev mode // from waiting on them for the page to load in dev mode
const __getStaticPaths = async () => { const __getStaticPaths = async () => {
const { publicRuntimeConfig, serverRuntimeConfig } = this.nextConfig const {
publicRuntimeConfig,
serverRuntimeConfig,
httpAgentOptions,
} = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.i18n || {} const { locales, defaultLocale } = this.nextConfig.i18n || {}
const paths = await this.staticPathsWorker.loadStaticPaths( const paths = await this.staticPathsWorker.loadStaticPaths(
...@@ -577,6 +581,7 @@ export default class DevServer extends Server { ...@@ -577,6 +581,7 @@ export default class DevServer extends Server {
publicRuntimeConfig, publicRuntimeConfig,
serverRuntimeConfig, serverRuntimeConfig,
}, },
httpAgentOptions,
locales, locales,
defaultLocale defaultLocale
) )
......
import { buildStaticPaths } from '../../build/utils' import { buildStaticPaths } from '../../build/utils'
import { setHttpAgentOptions } from '../config'
import { NextConfigComplete } from '../config-shared'
import { loadComponents } from '../load-components' import { loadComponents } from '../load-components'
import '../node-polyfill-fetch' import '../node-polyfill-fetch'
...@@ -14,6 +16,7 @@ export async function loadStaticPaths( ...@@ -14,6 +16,7 @@ export async function loadStaticPaths(
pathname: string, pathname: string,
serverless: boolean, serverless: boolean,
config: RuntimeConfig, config: RuntimeConfig,
httpAgentOptions: NextConfigComplete['httpAgentOptions'],
locales?: string[], locales?: string[],
defaultLocale?: string defaultLocale?: string
) { ) {
...@@ -25,6 +28,7 @@ export async function loadStaticPaths( ...@@ -25,6 +28,7 @@ export async function loadStaticPaths(
// update work memory runtime-config // update work memory runtime-config
require('../../shared/lib/runtime-config').setConfig(config) require('../../shared/lib/runtime-config').setConfig(config)
setHttpAgentOptions(httpAgentOptions)
const components = await loadComponents(distDir, pathname, serverless) const components = await loadComponents(distDir, pathname, serverless)
......
import fetch, { Headers, Request, Response } from 'node-fetch' import fetch, { Headers, Request, Response } from 'node-fetch'
import { Agent as HttpAgent } from 'http'
import { Agent as HttpsAgent } from 'https'
// Polyfill fetch() in the Node.js environment // Polyfill fetch() in the Node.js environment
if (!global.fetch) { if (!global.fetch) {
const httpAgent = new HttpAgent({ keepAlive: true })
const httpsAgent = new HttpsAgent({ keepAlive: true })
const agent = ({ protocol }) => const agent = ({ protocol }) =>
protocol === 'http:' ? httpAgent : httpsAgent protocol === 'http:' ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT
const fetchWithAgent = (url, opts, ...rest) => { const fetchWithAgent = (url, opts, ...rest) => {
if (!opts) { if (!opts) {
opts = { agent } opts = { agent }
......
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,
}
}
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
nextBuild, nextBuild,
findPort, findPort,
nextStart, nextStart,
launchApp,
killApp, killApp,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
...@@ -20,47 +21,81 @@ let app ...@@ -20,47 +21,81 @@ let app
let mockServer let mockServer
describe('node-fetch-keep-alive', () => { describe('node-fetch-keep-alive', () => {
beforeAll(async () => { describe('dev', () => {
mockServer = createServer((req, res) => { beforeAll(async () => {
// we can test request headers by sending them mockServer = createServer((req, res) => {
// back with the response // we can test request headers by sending them
const { connection } = req.headers // back with the response
res.end(JSON.stringify({ connection })) const { connection } = req.headers
res.end(JSON.stringify({ connection }))
})
mockServer.listen(44001)
appPort = await findPort()
app = await launchApp(appDir, appPort)
}) })
mockServer.listen(44001) afterAll(async () => {
const { stdout, stderr } = await nextBuild(appDir, [], { await killApp(app)
stdout: true, mockServer.close()
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()
})
it('should send keep-alive for json API', async () => { runTests()
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 () => { describe('production', () => {
const browser = await webdriver(appPort, '/ssg') beforeAll(async () => {
const props = await browser.elementById('props').text() mockServer = createServer((req, res) => {
const obj = JSON.parse(props) // we can test request headers by sending them
expect(obj).toEqual({ connection: 'keep-alive' }) // back with the response
await browser.close() 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()
})
it('should send keep-alive for getServerSideProps', async () => { runTests()
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()
}) })
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