Skip to content

Commit eabbc1a

Browse files
committed
fix(@angular/build): force HTTP/1.1 when using SSR with SSL
When using SSR with SSL, Vite attempts to use HTTP/2 by default. However, the Express server used for SSR does not support HTTP/2, causing requests to fail. This commit forces Vite to use HTTP/1.1 in this scenario by setting the ALPNProtocols to ['http/1.1']. Closes #31894
1 parent 61a027d commit eabbc1a

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/angular/build/src/builders/dev-server/vite/server.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ async function createServerConfig(
6060
headers: serverOptions.headers,
6161
// Disable the websocket if live reload is disabled (false/undefined are the only valid values)
6262
ws: serverOptions.liveReload === false && serverOptions.hmr === false ? false : undefined,
63-
// When server-side rendering (SSR) is enabled togather with SSL and Express is being used,
64-
// we must configure Vite to use HTTP/1.1.
65-
// This is necessary because Express does not support HTTP/2.
66-
// We achieve this by defining an empty proxy.
67-
// See: https://github.com/vitejs/vite/blob/c4b532cc900bf988073583511f57bd581755d5e3/packages/vite/src/node/http.ts#L106
68-
proxy:
69-
serverOptions.ssl && ssrMode === ServerSsrMode.ExternalSsrMiddleware ? (proxy ?? {}) : proxy,
63+
proxy,
7064
cors: {
7165
// This will add the header `Access-Control-Allow-Origin: http://example.com`,
7266
// where `http://example.com` is the requesting origin.

packages/angular/build/src/tools/vite/plugins/ssr-ssl-plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ export function createAngularServerSideSSLPlugin(): Plugin {
1313
return {
1414
name: 'angular-ssr-ssl-plugin',
1515
apply: 'serve',
16+
async configResolved(config) {
17+
// Force Vite to use HTTP/1.1 when SSR and SSL are enabled.
18+
// This is required because the Express server used for SSR does not support HTTP/2.
19+
// See: https://github.com/vitejs/vite/blob/46d3077f2b63771cc50230bc907c48f5773c00fb/packages/vite/src/node/http.ts#L126
20+
const https = config.server.https;
21+
if (https) {
22+
https.ALPNProtocols = ['http/1.1'];
23+
}
24+
},
1625
async configureServer({ config, httpServer }) {
1726
const {
1827
ssr,

0 commit comments

Comments
 (0)