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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
66408d8
polyfill undici
Ethan-Arrowood Sep 7, 2022
28a09b8
move polyfill to inside !global.fetch
Ethan-Arrowood Sep 7, 2022
0daf850
remove nodejs type update
Ethan-Arrowood Sep 7, 2022
1c5c027
update types and compile undici
Ethan-Arrowood Sep 7, 2022
f4169d7
try using global.__NEXT_USE_UNDICI and add tests
Ethan-Arrowood Sep 9, 2022
0b2475a
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Ethan-Arrowood Sep 9, 2022
07be524
remove auto-generated files
Ethan-Arrowood Sep 9, 2022
32d3677
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Ethan-Arrowood Sep 14, 2022
4cbc65f
Add ?. to useUndici
Ethan-Arrowood Sep 14, 2022
eb44e6b
Fix global config resolution
Ethan-Arrowood Sep 14, 2022
7fd115a
Fix global properties and add more tests
Ethan-Arrowood Sep 14, 2022
2a962a4
Update function name
Ethan-Arrowood Sep 15, 2022
6593733
remove unused code
Ethan-Arrowood 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
Ethan-Arrowood 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
Ethan-Arrowood Sep 16, 2022
60d99d7
fix lock file
Ethan-Arrowood Sep 16, 2022
0de3c26
Update packages/next/server/config.ts
Ethan-Arrowood Sep 16, 2022
989f9c8
fix semver usage
Ethan-Arrowood Sep 16, 2022
ed03b2b
useUndici -> enableUndici
Ethan-Arrowood Sep 16, 2022
421891b
Merge branch 'canary' into feature/polyfill-undici-fetch
Ethan-Arrowood Sep 16, 2022
acf39cb
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Ethan-Arrowood Sep 19, 2022
203c3e7
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
Ethan-Arrowood Sep 19, 2022
bbc2ac5
fix lock file
Ethan-Arrowood Sep 19, 2022
cbafb7f
Merge branch 'canary' into feature/polyfill-undici-fetch
Ethan-Arrowood 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
Ethan-Arrowood Sep 21, 2022
7da8b81
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
Ethan-Arrowood Sep 26, 2022
72c90fe
add missing argument in exportPage and revert compiled file changes
Ethan-Arrowood Sep 26, 2022
e5f3c60
remove compiled sass-loader file
Ethan-Arrowood Sep 26, 2022
fbc3e4d
Merge branch 'canary' into feature/polyfill-undici-fetch
Ethan-Arrowood 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/package.json
Expand Up @@ -75,6 +75,7 @@
"caniuse-lite": "^1.0.30001332",
"postcss": "8.4.14",
"styled-jsx": "5.0.6",
"undici": "5.10.0",
"use-sync-external-store": "1.2.0"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/config-utils.ts
Expand Up @@ -139,6 +139,7 @@ export function loadWebpackHook() {
'next/dist/compiled/@babel/runtime/package.json',
],
['node-fetch', 'next/dist/compiled/node-fetch'],
['undici', 'next/dist/compiled/undici'],
].map(
// Use dynamic require.resolve to avoid statically analyzable since they're only for build time
([request, replacement]) => [request, require.resolve(replacement)]
Expand Down
20 changes: 15 additions & 5 deletions packages/next/server/node-polyfill-fetch.js
@@ -1,8 +1,18 @@
import fetch, {
Headers,
Request,
Response,
} from 'next/dist/compiled/node-fetch'
let fetch, Headers, Request, Response

if (process.env.NEXT_USE_UNDICI) {
const undici = require('next/dist/compiled/undici')
fetch = undici.fetch
Headers = undici.Headers
Request = undici.Request
Response = undici.Response
} else {
const nodeFetch = require('next/dist/compiled/node-fetch')
fetch = nodeFetch.fetch
Headers = nodeFetch.Headers
Request = nodeFetch.Request
Response = nodeFetch.Response
}

// Polyfill fetch() in the Node.js environment
if (!global.fetch) {
Expand Down
10 changes: 10 additions & 0 deletions packages/next/taskfile.js
Expand Up @@ -86,6 +86,8 @@ const externals = {
'next/dist/build/webpack/plugins/terser-webpack-plugin',

// TODO: Add @swc/helpers to externals once @vercel/ncc switch to swc-loader

undici: 'undici',
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
}
// eslint-disable-next-line camelcase
externals['node-html-parser'] = 'next/dist/compiled/node-html-parser'
Expand Down Expand Up @@ -167,6 +169,14 @@ export async function ncc_node_fetch(task, opts) {
.target('compiled/node-fetch')
}

externals['undici'] = 'next/dist/compiled/undici'
export async function ncc_undici(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('undici')))
.ncc({ packageName: 'undici', externals })
.target('compiled/undici')
}

// eslint-disable-next-line camelcase
export async function compile_config_schema(task, opts) {
const { configSchema } = require('./dist/server/config-schema')
Expand Down
6 changes: 6 additions & 0 deletions packages/next/types/misc.d.ts
Expand Up @@ -28,6 +28,12 @@ declare module 'next/dist/compiled/node-fetch' {
export * from 'node-fetch'
}

declare module 'next/dist/compiled/undici' {
import m from 'undici'
export default m
export * from 'undici'
}

declare module 'next/dist/compiled/jest-worker' {
export * from 'jest-worker'
}
Expand Down