46e7658f7e
Follow up to #27376 so users can disable keep-alive. See comment https://github.com/vercel/next.js/pull/27376#issuecomment-885415623
19 lines
556 B
JavaScript
19 lines
556 B
JavaScript
import fetch, { Headers, Request, Response } from 'node-fetch'
|
|
|
|
// Polyfill fetch() in the Node.js environment
|
|
if (!global.fetch) {
|
|
const agent = ({ protocol }) =>
|
|
protocol === 'http:' ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT
|
|
const fetchWithAgent = (url, opts, ...rest) => {
|
|
if (!opts) {
|
|
opts = { agent }
|
|
} else if (!opts.agent) {
|
|
opts.agent = agent
|
|
}
|
|
return fetch(url, opts, ...rest)
|
|
}
|
|
global.fetch = fetchWithAgent
|
|
global.Headers = Headers
|
|
global.Request = Request
|
|
global.Response = Response
|
|
}
|