You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This will get node's implementation of fetch closer to how it behaves in the browser.
What is the feature you are proposing to solve the problem?
Support protocol upgrade similar to how fetch-h2 package does. It's weird to try to use a third-party package that uses node's internal http2 package under the hood.
What alternatives have you considered?
I am now using fetch-h2 package, but much like got-fetch or node-fetch I fear it will fall out of spec and not evolve with the ecosystem.
Reacted by Ankur Sethi, Nathan Brown, Sukka, Ben McCann, Elias Benali, Nils K, Steven, Trivikram Kamat, Andrii Oriekhov, JellyBrick and 4 more
@aduh95 Can you elaborate? Does nodeJs use undici? If yes, in what version is this supported? I'm using Node 20, which is latest LTE version and fetch there doesn't support H2.
I don't know much about HTTP/2, but here's an example that works in a browser (do it from the website itself to avoid CORS issue), while it fails in Node.js:
HTTP/2 support in undici is experimental and not enabled by default. Note that we do not support H2, but only HTTP/2 (over TLS), due the necessary protocol selection.
I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.
Good Idea! Repeating it for everybody:
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
Reacted by Carlos Fuentes, Joey Guerra, kojagiri1934, MeroFuruya, Peter Mbanugo, Robert Nagy, Trivikram Kamat and xuxu's code
@mcolina Just to clraify: I'm using Http2SecureServer with allowHttp1: true to return an error on unexpectedProtocol. So sounds like my server would always return the error in this proposal.
I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.
Good Idea! Repeating it for everybody:
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
like @mctrafik i also wanted to make a h2 request to an api endpoint
and just like him: the server allows h1 and responds with: 464 Incompatible protocol
mcolina Just to clraify: I'm using Http2SecureServer with allowHttp1: true to return an error on unexpectedProtocol. So sounds like my server would always return the error in this proposal.
But it ended up in a 2 hour rabbit hole trying to fix it.
First of all: The symbol are not available at first, it lazy loads,
secondly: changing the global dispatcher did not help, so i had to instead change the internal option in the default agent...
// undici global symbol are lazy loaded, so we need to trigger it firsttry{fetch('data:;base64,')}catch(e){}// Get the global agentconstunidiciGlobalDispatcherSymbol=Symbol.for('undici.globalDispatcher.1');constagent=globalThis[unidiciGlobalDispatcherSymbol]// get the agent internal optionsconstsymbols=Object.getOwnPropertySymbols(agent)const[kOption]=symbols.filter(predicate=>predicate.toString().includes('options'))constagentOptions=agent[kOption]// enable h2agentOptions.allowH2=true
now you can make h2 fetch (gRPC) request...
(gRPC do not support h1)
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
disagree, prefer HTTP/2 if available
browser, cloudflare and other have defaulted to using newer/shinier methods by default,
it seems like average load time can be reduced by switching to h2 by default?
In most cases, HTTP/2 is the better choice due to its improved efficiency, lower latency, and better resource management.
it use header compression
HTTP/2 can handle multiple requests over a single connection and reducing head-of-line blocking, it reduces the number of TCP connections, which lowers resource usage and improves performance
deno did prefer H2 by default without any changes which made it all much simpler without doing any hacks
(edit: the minimum requirement for it to use h2 instead of h1 was to use method: post in the fetch api call)
Reopening to discuss further for the next collab summit.
Note that the symbol thing is a hack and not officially supported. If you want to change that without pain, import undici and do setGlobalDispatcher() (in other words, you set yourself up for this specific pain, you are using undocumented features).
Activity
aduh95 commentedon Feb 12, 2024
Duplicate of #399. AFAICT it has already been implemented.
mctrafik commentedon Feb 13, 2024
@aduh95 Can you elaborate? Does nodeJs use undici? If yes, in what version is this supported? I'm using Node 20, which is latest LTE version and fetch there doesn't support H2.
aduh95 commentedon Feb 13, 2024
Correct (it is also maintained by us) https://github.com/nodejs/node/blob/109ab0a89cdeff5717a2a16edd27bafecc104cf6/doc/contributing/maintaining/maintaining-http.md#L54-L56
You can check what version of Undici your build is using in
process.versions.undici.Any chance you could send a repro? So we can reopen this issue and transfer it to the Undici repo.
targos commentedon Feb 13, 2024
I don't know much about HTTP/2, but here's an example that works in a browser (do it from the website itself to avoid CORS issue), while it fails in Node.js:
It's not easy to find a public URL that only works with HTTP/2.
aduh95 commentedon Feb 13, 2024
Created a minimal repro:
I can confirm the above code works with Deno and fails with the latest
main.mcollina commentedon Feb 13, 2024
HTTP/2 support in undici is experimental and not enabled by default. Note that we do not support H2, but only HTTP/2 (over TLS), due the necessary protocol selection.
To enable:
I think we could enable it in the next major and see how it goes. Unfortunately it might be breaking in case of bugs.
[-]`fetch` support for HTTP/2[/-][+]`fetch` support for HTTP/2 by default[/+]metcoder95 commentedon Feb 13, 2024
I believe it should be fine as soon as we keep it as
experimentalthough enabled by default when chosen by theserver.mcollina commentedon Feb 13, 2024
I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.
Good Idea! Repeating it for everybody:
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
mctrafik commentedon Feb 14, 2024
@mcolina Just to clraify: I'm using Http2SecureServer with
allowHttp1: trueto return an error onunexpectedProtocol. So sounds like my server would always return the error in this proposal.Can this behavior be controlled by a flag?!
mcollina commentedon Feb 14, 2024
@mctrafik you can already turn on http2 with:
metcoder95 commentedon Feb 14, 2024
SGTM 👍
6 remaining items
bmingles commentedon Nov 15, 2024
@mcollina Should the provided config work in node 20?
mcollina commentedon Nov 16, 2024
Yes
metcoder95 commentedon Dec 2, 2024
As
h2won't be enable by default but its out of experimental, shall we close this issue?mcollina commentedon Dec 2, 2024
Closing!
jimmywarting commentedon Mar 7, 2025
#developerPain
like @mctrafik i also wanted to make a h2 request to an api endpoint
and just like him: the server allows h1 and responds with:
464 Incompatible protocolI tried changing the global dispatcher:
But it ended up in a 2 hour rabbit hole trying to fix it.
First of all: The symbol are not available at first, it lazy loads,
secondly: changing the global dispatcher did not help, so i had to instead change the internal option in the default agent...
now you can make h2 fetch (gRPC) request...
(gRPC do not support h1)
disagree, prefer HTTP/2 if available
browser, cloudflare and other have defaulted to using newer/shinier methods by default,
it seems like average load time can be reduced by switching to h2 by default?
In most cases, HTTP/2 is the better choice due to its improved efficiency, lower latency, and better resource management.
it use header compression
HTTP/2 can handle multiple requests over a single connection and reducing head-of-line blocking, it reduces the number of TCP connections, which lowers resource usage and improves performance
deno did prefer H2 by default without any changes which made it all much simpler without doing any hacks
(edit: the minimum requirement for it to use h2 instead of h1 was to use method: post in the fetch api call)
mcollina commentedon Mar 8, 2025
Reopening to discuss further for the next collab summit.
Note that the symbol thing is a hack and not officially supported. If you want to change that without pain, import undici and do
setGlobalDispatcher()(in other words, you set yourself up for this specific pain, you are using undocumented features).jimmywarting commentedon Mar 8, 2025
yea, i know... but i rather prefer to use built in (without dependencies)
sibelius commentedon Apr 4, 2025
is there any docs for this ?
mcollina commentedon Apr 4, 2025
#2750 (comment)
mcollina commentedon Apr 4, 2025
At the collab summit, the majority was in favor of having HTTP/2 enabled by default in Node.js v25.
c410-f3r commentedon Jun 14, 2025
At the current time
allowH2still defaults tofalse.undici/lib/core/connect.js
Line 54 in 9eb2a2f
https://github.com/nodejs/node/blob/0b3fc0d7a80021b7c4db171bcd9be6036fe8d8e7/deps/undici/src/docs/docs/api/Client.md?plain=1#L32
Looking forward to seeing a flag flip now that the responsible individuals have voted in favour of the feature.
trivikr commentedon Apr 5, 2026
Undici 8.x now supports h2 by default.
This was enabled in #4828
I believe this issue can be closed.