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(dev): modify websocket client port #1926

Closed
wants to merge 8 commits into from
Closed

Conversation

ygj6
Copy link
Member

@ygj6 ygj6 commented Feb 7, 2021

close #1925

Modify client websocket port from __HMR_PORT__ to location.port .
Websocket client will connect to frontend port instead of backend port when there is a reverse proxy.

@bompus
Copy link
Contributor

bompus commented Feb 7, 2021

@ygj6 #1925 did you try using https://vitejs.dev/config/#server-hmr config options first?

From looking at the code - HMR_PORT is controlled by config.server.hmr.port

server: {
      port: 3000,
      https: true,
      hmr: {
        host: 'yourhost.local',
        port: 3001,
        protocol: 'wss'
      }
    }
}

@ygj6
Copy link
Member Author

ygj6 commented Feb 8, 2021

@ygj6 #1925 did you try using https://vitejs.dev/config/#server-hmr config options first?

From looking at the code - HMR_PORT is controlled by config.server.hmr.port

server: {
      port: 3000,
      https: true,
      hmr: {
        host: 'yourhost.local',
        port: 3001,
        protocol: 'wss'
      }
    }
}

Good. This config can solve problem in this case.
But I think this config option makes the reverse proxy meaningless.

@bompus
Copy link
Contributor

bompus commented Feb 8, 2021

If you still want to go through the reverse proxy on port 3000, wouldn't you just use the same port for both server and server.hmr and just make sure the hmr.protocol : "wss" and hmr.host is set to what ip/host you are connecting to?

@edorgeville
Copy link

edorgeville commented Feb 25, 2021

This PR would solve my use case:
I have a Vite service that is accessed through port 80 on LAN, but we have a seperate reverse proxy configured that gives us access on a public facing URL over HTTPS.
Currently, if you access Vite locally on port 80, everything works correctly.
But if you try to access it through the reverse proxy (HTTPS, port 443), Vite's HMR tries to connect to port 80 ( __HMR_PORT__) and fails.
Changing __HMR_PORT__ to port 443 in the config would break the local access as well.

@fuzzykiller
Copy link

Implementing this change would enable a dev server proxy middleware for ASP.NET Core. Because the port chosen for the dev server is dynamic, a config file option is not really an appropriate solution. Additionally, the Vite.js dev server may not be running with HTTPS enabled when the reverse proxy is.

A fully “relative” WebSocket would solve all this and make the HMR host/port option obsolete.

@Shinigami92 Shinigami92 added p3-minor-bug An edge case that only affects very specific usage (priority) 🔍 review needed labels Mar 23, 2021
@@ -15,7 +15,7 @@ console.log('[vite] connecting...')
// use server configuration, then fallback to inference
const socketProtocol =
__HMR_PROTOCOL__ || (location.protocol === 'https:' ? 'wss' : 'ws')
const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`
const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${location.port}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${location.port}`
const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__ || location.port}`

Think this could be better. Respect server.hmr.port if specified explicitly or use the same port as client. clientInject.ts file needs to be changed accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Client websocket connect failed when using https option and reverse proxy
10 participants