Skip to content

fetch support for HTTP/2 by default #2750

Closed

Description

@mctrafik

What is the problem this feature will solve?

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.

Activity

  1. aduh95 commented on Feb 12, 2024

    @aduh95
    Contributor

    Duplicate of #399. AFAICT it has already been implemented.

  2. mctrafik commented on Feb 13, 2024

    @mctrafik
    Author

    @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.

  3. aduh95 commented on Feb 13, 2024

    @aduh95
    Contributor

    Does nodeJs use undici?

    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.

    I'm using Node 20, which is latest LTE version and fetch there doesn't support H2.

    Any chance you could send a repro? So we can reopen this issue and transfer it to the Undici repo.

  4. targos commented on Feb 13, 2024

    @targos
    Member

    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:

    fetch('https://api.sandbox.push.apple.com').then(r => r.text()).then(console.log)
    

    It's not easy to find a public URL that only works with HTTP/2.

  5. aduh95 commented on Feb 13, 2024

    @aduh95
    Contributor

    Created a minimal repro:

    import { createServer, constants } from 'node:http2';
    import { once } from 'node:events';
    
    const {
      HTTP2_HEADER_STATUS,
      HTTP2_HEADER_CONTENT_TYPE,
    } = constants;
    
    const server = createServer();
    
    server.on('stream', (stream, headers) => {
      stream.respond({
        [HTTP2_HEADER_STATUS]: 200,
        [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8',
        'Access-Control-Allow-Origin': '*',
      });
      stream.write('hello world');
      stream.end();
    });
    
    
    server.listen(3000);
    await once(server, 'listening');
    
    const response = await fetch('http://localhost:3000/');
    
    console.assert(response.ok, response.status);
    console.log(await response.text());
    
    server.close();

    I can confirm the above code works with Deno and fails with the latest main.

  6. reopened this on Feb 13, 2024
  7. transferred this issue fromnodejs/nodeon Feb 13, 2024
  8. mcollina commented on Feb 13, 2024

    @mcollina
    SponsorMember

    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:

    import { createServer, constants } from 'node:http2';
    import { once } from 'node:events';
    import { setGlobalDispatcher, Agent } from './index.js';
    
    const {
      HTTP2_HEADER_STATUS,
      HTTP2_HEADER_CONTENT_TYPE,
    } = constants;
    
    setGlobalDispatcher(new Agent({
      allowH2: true
    }));
    
    fetch('https://api.sandbox.push.apple.com').then(r => r.text()).then(console.log)

    I think we could enable it in the next major and see how it goes. Unfortunately it might be breaking in case of bugs.

  9. changed the title [-]`fetch` support for HTTP/2[/-] [+]`fetch` support for HTTP/2 by default[/+] on Feb 13, 2024
  10. metcoder95 commented on Feb 13, 2024

    @metcoder95
    Member

    I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.

  11. mcollina commented on Feb 13, 2024

    @mcollina
    SponsorMember

    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?

  12. mctrafik commented on Feb 14, 2024

    @mctrafik
    Author

    @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.

    Can this behavior be controlled by a flag?!

  13. mcollina commented on Feb 14, 2024

    @mcollina
    SponsorMember

    @mctrafik you can already turn on http2 with:

    import { setGlobalDispatcher, Agent } from 'undici';
    
    setGlobalDispatcher(new Agent({
      allowH2: true
    }));
  14. metcoder95 commented on Feb 14, 2024

    @metcoder95
    Member

    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?

    SGTM 👍

  15. 6 remaining items

  16. bmingles commented on Nov 15, 2024

    @bmingles

    @mcollina Should the provided config work in node 20?

    setGlobalDispatcher(new Agent({
      allowH2: true
    }));
  17. mcollina commented on Nov 16, 2024

    @mcollina
    SponsorMember

    Yes

  18. metcoder95 commented on Dec 2, 2024

    @metcoder95
    Member

    As h2 won't be enable by default but its out of experimental, shall we close this issue?

  19. mcollina commented on Dec 2, 2024

    @mcollina
    SponsorMember

    Closing!

  20. jimmywarting commented on Mar 7, 2025

    @jimmywarting
    Contributor

    #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 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.

    I tried changing the global dispatcher:

    global[Symbol.for("undici.globalDispatcher.1")] = new global[Symbol.for("undici.globalDispatcher.1")].constructor({
    	allowH2: true,
    });

    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 first
    try { fetch('data:;base64,') } catch (e) { }
    
    // Get the global agent
    const unidiciGlobalDispatcherSymbol = Symbol.for('undici.globalDispatcher.1');
    const agent = globalThis[unidiciGlobalDispatcherSymbol]
    
    // get the agent internal options
    const symbols = Object.getOwnPropertySymbols(agent)
    const [kOption] = symbols.filter(predicate => predicate.toString().includes('options'))
    const agentOptions = agent[kOption]
    
    // enable h2
    agentOptions.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)

  21. mcollina commented on Mar 8, 2025

    @mcollina
    SponsorMember

    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).

  22. jimmywarting commented on Mar 8, 2025

    @jimmywarting
    Contributor

    import undici and do setGlobalDispatcher() (in other words, you set yourself up for this specific pain, you are using undocumented features).

    yea, i know... but i rather prefer to use built in (without dependencies)

  23. sibelius commented on Apr 4, 2025

    @sibelius

    is there any docs for this ?

  24. mcollina commented on Apr 4, 2025

    @mcollina
    SponsorMember
  25. mcollina commented on Apr 4, 2025

    @mcollina
    SponsorMember

    At the collab summit, the majority was in favor of having HTTP/2 enabled by default in Node.js v25.

  26. c410-f3r commented on Jun 14, 2025

    @c410-f3r

    At the current time allowH2 still defaults to false.

    allowH2 = allowH2 != null ? allowH2 : false

    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.

  27. trivikr commented on Apr 5, 2026

    @trivikr
    Member

    Undici 8.x now supports h2 by default.
    This was enabled in #4828

    I believe this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions