Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ Set to `true` to exit if port is already in use, instead of automatically trying

Enable TLS + HTTP/2. The value is an [options object](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) passed to `https.createServer()`.

Note that this downgrades to TLS only when the [`server.proxy` option](#server-proxy) is also used.

A valid certificate is needed. For a basic setup, you can add [@vitejs/plugin-basic-ssl](https://github.com/vitejs/vite-plugin-basic-ssl) to the project plugins, which will automatically create and cache a self-signed certificate. But we recommend creating your own certificates.

## server.open
Expand Down
31 changes: 12 additions & 19 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export interface CorsOptions {
export type CorsOrigin = boolean | string | RegExp | (string | RegExp)[]

export async function resolveHttpServer(
{ proxy }: CommonServerOptions,
app: Connect.Server,
httpsOptions?: HttpsServerOptions,
): Promise<HttpServer> {
Expand All @@ -124,24 +123,18 @@ export async function resolveHttpServer(
return createServer(app)
}

// #484 fallback to http1 when proxy is needed.
if (proxy) {
const { createServer } = await import('node:https')
return createServer(httpsOptions, app)
} else {
const { createSecureServer } = await import('node:http2')
return createSecureServer(
{
// Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM
// errors on large numbers of requests
maxSessionMemory: 1000,
...httpsOptions,
allowHTTP1: true,
},
// @ts-expect-error TODO: is this correct?
app,
)
}
const { createSecureServer } = await import('node:http2')
return createSecureServer(
{
// Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM
// errors on large numbers of requests
maxSessionMemory: 1000,
...httpsOptions,
allowHTTP1: true,
},
// @ts-expect-error TODO: is this correct?
app,
)
}

export async function resolveHttpsConfig(
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function preview(

const httpsOptions = await resolveHttpsConfig(config.preview.https)
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(config.preview, app, httpsOptions)
const httpServer = await resolveHttpServer(app, httpsOptions)
setClientErrorHandler(httpServer, config.logger)

const options = config.preview
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export async function _createServer(
const middlewares = connect() as Connect.Server
const httpServer = middlewareMode
? null
: await resolveHttpServer(serverConfig, middlewares, httpsOptions)
: await resolveHttpServer(middlewares, httpsOptions)

const ws = createWebSocketServer(httpServer, config, httpsOptions)

Expand Down
Loading