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

fix: consider undefined port when checking port #7318

Merged
merged 9 commits into from Mar 23, 2022
11 changes: 5 additions & 6 deletions packages/vite/src/node/server/ws.ts
Expand Up @@ -28,9 +28,9 @@ export function createWebSocketServer(
let httpsServer: Server | undefined = undefined

const hmr = isObject(config.server.hmr) && config.server.hmr
const wsServer =
(hmr && hmr.server) ||
((!(hmr && hmr.port) || hmr.port !== config.server.port) && server)
const hmrServer = hmr && hmr.server
const hmrPort = (hmr && hmr.port) || 24678
const wsServer = hmrServer || (hmrPort !== config.server.port && server)

if (wsServer) {
wss = new WebSocket({ noServer: true })
Expand All @@ -43,7 +43,6 @@ export function createWebSocketServer(
})
} else {
const websocketServerOptions: ServerOptions = {}
const port = (hmr && hmr.port) || 24678
const host = (hmr && hmr.host) || undefined
if (httpsOptions) {
// if we're serving the middlewares over https, the ws library doesn't support automatically creating an https server, so we need to do it ourselves
Expand All @@ -63,11 +62,11 @@ export function createWebSocketServer(
res.end(body)
})

httpsServer.listen(port, host)
httpsServer.listen(hmrPort, host)
websocketServerOptions.server = httpsServer
} else {
// we don't need to serve over https, just let ws handle its own server
websocketServerOptions.port = port
websocketServerOptions.port = hmrPort
if (host) {
websocketServerOptions.host = host
}
Expand Down