Disabling HTTP Keep-Alive

Next.js automatically polyfills node-fetch and enables HTTP 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:

  1. import { Agent } from 'https'
  2. const url = 'https://example.com'
  3. const agent = new Agent({ keepAlive: false })
  4. fetch(url, { agent })

To override all fetch() calls globally, you can use next.config.js:

  1. module.exports = {
  2. httpAgentOptions: {
  3. keepAlive: false,
  4. },
  5. }

Introduction to next.config.jsLearn more about the configuration file used by Next.js.