Skip to content

Commit 0bd4426

Browse files
fix: support custom servers using HTTP/2 in production (#12989)
Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com>
1 parent 7a17bd2 commit 0bd4426

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

.changeset/shy-needles-warn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: support HTTP/2 in dev and production. Revert the changes from [#12907](https://github.com/sveltejs/kit/pull/12907) to downgrade HTTP/2 to TLS as now being unnecessary

packages/kit/src/exports/node/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,25 @@ function get_raw_body(req, body_size_limit) {
106106
// TODO 3.0 make the signature synchronous?
107107
// eslint-disable-next-line @typescript-eslint/require-await
108108
export async function getRequest({ request, base, bodySizeLimit }) {
109+
let headers = /** @type {Record<string, string>} */ (request.headers);
110+
if (request.httpVersionMajor >= 2) {
111+
// the Request constructor rejects headers with ':' in the name
112+
headers = Object.assign({}, headers);
113+
// https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3.1-2.3.5
114+
if (headers[':authority']) {
115+
headers.host = headers[':authority'];
116+
}
117+
delete headers[':authority'];
118+
delete headers[':method'];
119+
delete headers[':path'];
120+
delete headers[':scheme'];
121+
}
122+
109123
return new Request(base + request.url, {
110124
// @ts-expect-error
111125
duplex: 'half',
112126
method: request.method,
113-
headers: /** @type {Record<string, string>} */ (request.headers),
127+
headers: Object.entries(headers),
114128
body:
115129
request.method === 'GET' || request.method === 'HEAD'
116130
? undefined

packages/kit/src/exports/vite/index.js

-16
Original file line numberDiff line numberDiff line change
@@ -349,22 +349,6 @@ async function kit({ svelte_config }) {
349349
* Stores the final config.
350350
*/
351351
configResolved(config) {
352-
// we search for this plugin by name because we can't detect it
353-
// since it doesn't directly modify the https config unlike the mkcert plugin
354-
const vite_basic_ssl = config.plugins.find(({ name }) => name === 'vite:basic-ssl');
355-
356-
// by default, when enabling HTTPS in Vite, it also enables HTTP/2
357-
// however, undici has not yet enabled HTTP/2 by default: https://github.com/nodejs/undici/issues/2750
358-
// we set a no-op proxy config to force Vite to downgrade to TLS-only
359-
// see https://vitejs.dev/config/#server-https
360-
if ((config.server.https || vite_basic_ssl) && !config.server.proxy) {
361-
config.server.proxy = {};
362-
}
363-
364-
if ((config.preview.https || vite_basic_ssl) && !config.preview.proxy) {
365-
config.preview.proxy = {};
366-
}
367-
368352
vite_config = config;
369353
}
370354
};

packages/kit/src/exports/vite/preview/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export async function preview(vite, vite_config, svelte_config) {
185185

186186
// SSR
187187
vite.middlewares.use(async (req, res) => {
188-
const host = req.headers['host'];
188+
const host = req.headers[':authority'] || req.headers.host;
189189

190190
const request = await getRequest({
191191
base: `${protocol}://${host}`,

0 commit comments

Comments
 (0)