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

feat(experimental): option to polyfill fetch using undici in Node.js <18 #40318

Merged
merged 61 commits into from
Sep 27, 2022
Merged
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
66408d8
polyfill undici
Sep 7, 2022
28a09b8
move polyfill to inside !global.fetch
Sep 7, 2022
0daf850
remove nodejs type update
Sep 7, 2022
1c5c027
update types and compile undici
Sep 7, 2022
f4169d7
try using global.__NEXT_USE_UNDICI and add tests
Sep 9, 2022
0b2475a
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Sep 9, 2022
07be524
remove auto-generated files
Sep 9, 2022
32d3677
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Sep 14, 2022
4cbc65f
Add ?. to useUndici
Sep 14, 2022
eb44e6b
Fix global config resolution
Sep 14, 2022
7fd115a
Fix global properties and add more tests
Sep 14, 2022
2a962a4
Update function name
Sep 15, 2022
6593733
remove unused code
Sep 15, 2022
a11d117
require fetch implementation conditionally
balazsorban44 Sep 15, 2022
ebc33d7
update lock file
balazsorban44 Sep 15, 2022
f623f65
add `keepAlive` tests for `undici`
balazsorban44 Sep 15, 2022
502db7f
handle `keepAlive` with `undici`
balazsorban44 Sep 15, 2022
88c65cd
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 Sep 15, 2022
1651916
update compiled
balazsorban44 Sep 15, 2022
8393aaf
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
balazsorban44 Sep 15, 2022
76d7b6c
Merge branch 'canary' into feature/polyfill-undici-fetch
Sep 15, 2022
367862f
add back `mini-css-extract-plugin` compiled
balazsorban44 Sep 16, 2022
3866700
revert compiled
balazsorban44 Sep 16, 2022
58ecb4c
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 Sep 16, 2022
454d917
revert webpack compiled changes
balazsorban44 Sep 16, 2022
1b26d54
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
balazsorban44 Sep 16, 2022
0de1fb4
revert
balazsorban44 Sep 16, 2022
ec21630
revert
balazsorban44 Sep 16, 2022
e8550fa
revert?
balazsorban44 Sep 16, 2022
7e6b1f9
more revert
balazsorban44 Sep 16, 2022
4919546
one more
balazsorban44 Sep 16, 2022
ef8b44f
update taskfile
balazsorban44 Sep 16, 2022
789b43b
update compiled
balazsorban44 Sep 16, 2022
245ff7b
Add version guard for <16.8.0
Sep 16, 2022
60d99d7
fix lock file
Sep 16, 2022
0de3c26
Update packages/next/server/config.ts
Ethan-Arrowood Sep 16, 2022
989f9c8
fix semver usage
Sep 16, 2022
ed03b2b
useUndici -> enableUndici
Sep 16, 2022
421891b
Merge branch 'canary' into feature/polyfill-undici-fetch
Sep 16, 2022
acf39cb
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Sep 19, 2022
203c3e7
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
Sep 19, 2022
bbc2ac5
fix lock file
Sep 19, 2022
cbafb7f
Merge branch 'canary' into feature/polyfill-undici-fetch
Sep 19, 2022
d2087ce
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk Sep 21, 2022
f770bd5
update lock
ijjk Sep 21, 2022
31beccc
update compiled
ijjk Sep 21, 2022
7722ea5
Update packages/next/server/node-polyfill-fetch.js
Ethan-Arrowood Sep 21, 2022
424531a
add enableUndici to worker exportPage
Sep 21, 2022
7da8b81
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Sep 26, 2022
72c90fe
add missing argument in exportPage and revert compiled file changes
Sep 26, 2022
e5f3c60
remove compiled sass-loader file
Sep 26, 2022
fbc3e4d
Merge branch 'canary' into feature/polyfill-undici-fetch
Sep 26, 2022
fcfa989
update lock file
balazsorban44 Sep 26, 2022
08fefca
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 Sep 26, 2022
b101ec6
update compiled
balazsorban44 Sep 26, 2022
82a0961
fix test
balazsorban44 Sep 26, 2022
ef18f28
tweak `enableUndici` warnings
balazsorban44 Sep 27, 2022
05ca18f
pass `enableUndici` to worker
balazsorban44 Sep 27, 2022
519c717
tweak test
balazsorban44 Sep 27, 2022
6002dba
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk Sep 27, 2022
8ed03ee
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk Sep 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
require fetch implementation conditionally
  • Loading branch information
balazsorban44 committed Sep 15, 2022
commit a11d11749125f5e39d69a2221ccf65adeec4f8c1
42 changes: 24 additions & 18 deletions packages/next/server/node-polyfill-fetch.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
// Polyfill fetch() in the Node.js environment

if (!global.fetch) {
// Due to limitation of global configuartion. we have to do this resolution at runtime
const nodeFetch = require('next/dist/compiled/node-fetch')
const undici = require('next/dist/compiled/undici')
function getFetchImpl() {
return global.__NEXT_USE_UNDICI
? require('next/dist/compiled/undici')
: require('next/dist/compiled/node-fetch')
}
// Due to limitation of global configuartion, we have to do this resolution at runtime
global.fetch = (...args) => {
const fetchImpl = getFetchImpl()

if (global.__NEXT_USE_UNDICI) {
return undici.fetch(...args)
} else {
const agent = ({ protocol }) =>
protocol === 'http:'
? global.__NEXT_HTTP_AGENT
: global.__NEXT_HTTPS_AGENT
return fetchImpl.fetch(...args)
}
const agent = ({ protocol }) =>
Copy link
Member

Choose a reason for hiding this comment

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

This could be a problem because the agent is created for every fetch(). It should be reused, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the Agent is created and assigned to global.__NEXT_HTTP_AGENT or global.__NEXT_HTTPS_AGENT. This function just returns the correct agent every time fetch is called.

But since we can't do that for the Undici agent we have to have all that !global.__NEXT_UNDICI_AGENT_SET logic.

protocol === 'http:'
? global.__NEXT_HTTP_AGENT
: global.__NEXT_HTTPS_AGENT

if (!args[1]) {
args[1] = { agent }
} else if (!args[1].agent) {
args[1].agent = agent
}
return nodeFetch(...args)
if (!args[1]) {
args[1] = { agent }
} else if (!args[1].agent) {
args[1].agent = agent
}

return fetchImpl(...args)
}

Object.defineProperties(global, {
Headers: {
get() {
return global.__NEXT_USE_UNDICI ? undici.Headers : nodeFetch.Headers
return getFetchImpl().Headers
},
},
Request: {
get() {
return global.__NEXT_USE_UNDICI ? undici.Request : nodeFetch.Request
return getFetchImpl().Request
},
},
Response: {
get() {
return global.__NEXT_USE_UNDICI ? undici.Response : nodeFetch.Response
return getFetchImpl().Response
},
},
})
Expand Down