Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update disabling-http-keep-alive.md for server-side only #43374

Merged
merged 5 commits into from Dec 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 2 additions & 12 deletions docs/api-reference/next.config.js/disabling-http-keep-alive.md
Expand Up @@ -4,19 +4,9 @@ description: Next.js will automatically use HTTP Keep-Alive by default. Learn mo

# 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.
In Node.js versions prior to 18, Next.js automatically polyfills `fetch()` with [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.

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`:
To disable HTTP Keep-Alive for all `fetch()` calls on the server-side, open `next.config.js` and add the `httpAgentOptions` config:

```js
module.exports = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this uses the same Agent behavior so it probably wont work either on Node.js 18

cc @Ethan-Arrowood who implemented undici and might know more

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our testing, unfortunately these agent options aren't compatible with undici and there's no equivalent alternative.

We had to intentionally disable Node 18's undici with --no-experimental-fetch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undici's fetch accepts dispatcher which fulfills a similar role as agent though it requires undici specific Pool/Agent/etc instead of node's http agent.

Expand Down