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

Add support for HTTP/2 #1451

Open
Zerotask opened this issue May 14, 2021 · 7 comments
Open

Add support for HTTP/2 #1451

Zerotask opened this issue May 14, 2021 · 7 comments
Labels
Milestone

Comments

@Zerotask
Copy link
Contributor

I just did a test with Lighthouse (Desktop) and it showed incredibely nice results on the first try. This is really awesome.
image

For the Performance section the major penalty was missing HTTP\2
image

Describe the solution you'd like
I'd like to have an option to activate HTTP\2 via config.

@Conduitry
Copy link
Member

I'm adding the pkg:adapter-node label to this, because I don't think the question of supporting HTTP/2 really makes sense in other contexts.

@dreitzner
Copy link
Contributor

This is currently not possible as polka (current server in use) does not support http2.

Maybe we can get a polka update with an http2 option....

@Zerotask
Copy link
Contributor Author

Zerotask commented May 14, 2021

I created a feature request there: lukeed/polka#162

Since http2 is already supported by Node.js, it's more a framework and design decision for polka.

edit:
I noticed that the last human commit is from Aug 17, 2020. So as an alternative, we could also consider using a more up to date framework which already supports HTTP\2 like https://github.com/fastify/fastify

Hi there, it's already supported.
Do the exact same thing as the https example, but use the http2 module instead of the https module.
Once you create your own server, you pass it into your (main) Polka instance as options.server
Hope that helps~!

@jycouet
Copy link
Contributor

jycouet commented Feb 20, 2022

@Rich-Harris Rich-Harris added this to the whenever milestone Apr 25, 2022
@Rich-Harris Rich-Harris added the feature / enhancement New feature or request label Apr 25, 2022
@Zerotask Zerotask changed the title Add support for HTTP\2 Add support for HTTP/2 Aug 12, 2023
@bluepuma77
Copy link

The pull request was denied, one of the reasons was TLS. Maybe it would be possible to start without TLS?

Enable HTTP/2.0 (h2c) without TLS in SvelteKit, so users can benefit from decreased latencies and improved page load speeds. With this the usual reverse proxy can handle TLS, and we can still benefit from the http/2 speed improvements on the internal network.

Reference: HTTP/2.0 h2c on Wikipedia

@aradalvand
Copy link
Contributor

@bluepuma77 Your best bet is just using a custom server, they're not gonna add built-in support for this.

@dlebech
Copy link

dlebech commented Nov 8, 2024

http/2 in unencrypted cleartext (h2c) works on Node 20.12.0 and earlier with a custom polka server. Support broke between v20.12.0 to v20.12.1, which was discussed over here: #11365 (comment)

Anyway, for the record, here is how I run h2c on node v20.12.0:

  1. Create a server.js file at the root of your sveltekit project
  2. Use the following contents:
import polka from 'polka';
import http2 from 'node:http2';
import { handler } from './build/handler.js';
import { env } from './build/env.js';

const path = env('SOCKET_PATH', false);
const host = env('HOST', '0.0.0.0');
const port = env('PORT', !path && '3000');

// Note: http2.createServer() is unencrypted and will not work in any browser
// This server should be behind a reverse proxy (eg. NGINX) with SSL termination.
const server = polka({ server: http2.createServer() })
  .use(handler)
  .listen({ path, host, port }, () => {
    console.log(`Listening on ${path ? path : host + ':' + port}`);
  });

const sessions = new Set();

server.server.on('session', (session) => {
  sessions.add(session);
  session.on('close', () => {
    sessions.delete(session);
  });
});

function shutdown() {
  server.server.close(() => {
    console.log('Server has closed.');
  });

  for (const session of sessions) {
    session.close();
  }
}

process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
process.on('exit', () => {
  console.log('Process is exiting.');
});
  1. To test locally
    1. Run with e.g. PORT=1234 node server.js
    2. Use curl command curl -i --http2-prior-knowledge http://localhost:1234

Again, the above will only work in Node 20.12.0 and below, unfortunately. Running an old version of Node is probably not the best idea long-term, so it would be nice with a better solution for h2c http/2.

(PS. I'm using Google Cloud Run for end-to-end http/2 which requires h2c for it to work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants