Replies: 14 comments 14 replies
-
I had the same issue, ended up using it with docker and ssl. The issue was that when creating the socket server over ssl it wasn't using the cert/key used by the express app. So using Vite server like this fixed the issue: createServer({
root: appDir,
server: {
https: {
key: fs.readFileSync(pathToKey),
cert: fs.readFileSync(pathToCert),
},
middlewareMode: "html"
}
}) |
Beta Was this translation helpful? Give feedback.
-
Same issue. After lots of try's I was able to solve it. /etc/hosts: nginx in a docker container with ssl: (certificate created with vishnunair/docker-mkcert) upstream webapp-upstream{ upstream wss-upstream{ server { location /socket.io{ location /{ And vite.config.ts Hope it helps you |
Beta Was this translation helpful? Give feedback.
-
this was working for me behind a local caddy reverse proxy to port 3000 that automatically provides ssl in development:
but I upgraded from vite |
Beta Was this translation helpful? Give feedback.
-
I removed |
Beta Was this translation helpful? Give feedback.
-
You don't need to change anything in the default You don't need to proxy to Fully functional apache reverse proxy
EDIT: The relevant part, all you need to do is this, to configure the
|
Beta Was this translation helpful? Give feedback.
-
Here is the complete solution (web app on port 3001, nginx handing SSL, HMR on port 3004). I used zeitler's solution / post as a starting point: In your vite.config:
As for nginx, add these configs to your nginx.conf:
You must then make sure that nginx can resolve host.docker.internal to your host's IP address. Easiest way is to add this to your docker compose service config:
Or equivalent in docker if you don't use compose. This is not trivial, took me more than an hour to figure this out - hope it helps someone out there. |
Beta Was this translation helpful? Give feedback.
-
Here is traefik example, to get it working nuxt3 with vite and hot module replacement (hmr): https://github.com/nuxt/framework/issues/1796#issuecomment-1365911503 |
Beta Was this translation helpful? Give feedback.
-
If you're using nginx, you just need to enable websocket support by adding Upgrade and Connection headers to your proxy
|
Beta Was this translation helpful? Give feedback.
-
Same problem here: unable to use HMR through https proxy. Easy step to reproduce:
I tried every vite server / hmr / proxy / host settings without success. The same occurs from a remote docker. |
Beta Was this translation helpful? Give feedback.
-
Thanks for at least posting this issue and also researching the possible solutions. I have a remote dev setup with k8s, where my Vue+Vite app is inside a pod and I also have a special setup to be able to develop with VSCode remotely in it. With k8s, the ingress system is basically a reverse proxy and I was having issues with Vite's HMR websocket connection. Your tip with Again, thanks! Scott |
Beta Was this translation helpful? Give feedback.
-
May be good suggestion is to host docker server on http and just make some gateway container which cares about https, usually, you don't have https between microservices (your proxy +your frontend == microservices) to save energy. |
Beta Was this translation helpful? Give feedback.
-
After hours of debugging, I found out that in my case the issue was that the reverse proxy was terminating the connection after 6 seconds (because there was no data exchange within 6 seconds) and this apparently the normal behaviour for Nginx The documentation for Nginx states that:
In the default configuration, 60s is enough for the client to start pinging the server, however, I am using APISIX as reverse proxy which set the default timeout of 6s so it disconnected well before the first ping. In case that you notice that the websocket connects, but drops shortly after, check the timeout and increase it to 30-40s |
Beta Was this translation helpful? Give feedback.
-
For me the solution was to add vite: {
server: {
hmr: {
clientPort: 443,
protocol: 'wss',
path: 'ws',
}
},
} |
Beta Was this translation helpful? Give feedback.
-
This new configuration saved my life again, the previous configuration no longer works.... so simple... and it seemed so obvious as soon as I read it... and it worked. Thanks ChatGPT ! Tested with Nuxt 3.13.2 in SSR mode
setup Nginx as SSL proxy
That's all. |
Beta Was this translation helpful? Give feedback.
-
As soon as you put
yarn dev
ornpm run dev
behind a https proxy, it's impossible for vite to provide HMR due to a variety of implementation caveats and poor documentation. I could split this bug into several, but it's all HMR and it's confusing set of configuration options and fallbacks.I'm using the vite (2.7.10) + vue-ts template to create a project.
Example 1:
hmr: false
Adding this option to vite.config.js does nothing, or at least nothing I can detect. The front-end still tries to connect to the vite websocket server for hmr, but on the default
:3000
port.Example 2:
This makes the front-end code connect to the correct address, e.g.
wss://domain
instead ofwss://domain:3000
. No idea why it's being rejected. The javascript stack isn't useful (client.ts:28)Example 3:
Now, this is the most confusing, we have
hmr.port
andhmr.server.port
. Thehmr.server
options lead me to believe that a separate server would be started if I provide e.g.hmr: { server: { port: 3001 } }
. They don't seem to have any effect, only the main service comes online (:3000). It's all guesswork due to the very brief docs for the hmr json schema.Digging around the code, a server is started if HTTPS options are provided. I don't need to provide HTTPS options as it's being terminated on my own edge servers, but it should be started in http mode too?
Other:
I think
hmr.clientPort
works, the client protocol is nicely autodetected towss
, there's just a connectivity issue and I can't figure out what.I've tried returning an error code (404, 500, 502, 503) for
/__vite_ping
and thereby avoiding the constant reloading on the client, and it seems to be fully ignored by the front end client:My suggestion would be to create a RFC, document HMR use cases e.g. vite.config.js for the following:
wss://domain.tld
)hmr.path
, is it just a client-side change? Is it relevant? How?I'm sorry if the frustration is coming out of the above text, I've spent a day to get this working without errors without success, and my best bet at resolving this is modifiying files in node_modules. When I'm frustrated, I rant. My suggestion would be to implement HMR, or at least the server/client and config part of it, by introducing a RFC process.
It's critical to document common use cases (e.g. standalone, docker, https), and provide better documentation than what exists for HMR currently, and examples to satisfy the above cases. There are a couple of PRs open, which could mitigate my issue. One of them is #6090 where I could disable this retry/refresh behavior, and just live without HMR. Is it great? No. Would I appreciate this being merged when this day started? YES.
Oh and additionally, if you enable middlewareMode, there's a bug along the lines of
wsServer.on
doesn't exists. I'm unsure what the point was, but if it was attaching an "upgrade" listener for websockets to the default server, it's probablly not passed correctly or something. I was tempted to modify this intowss.on('upgrade'...
but it didn't work, apart from getting rid of the notice, it's been a no-op, like all of my work on this today.If anybody is running HMR behind a third party https proxy (and not localhost/, along the lines of ngrok.io, or an nginx that's terminating SSL) - I'd be very interested in what you had to do to make it work. Some of my workaround attempts involved a nginx-proxy in front, which would route websocket requests to :3001 - before realizing that
hmr.server.port
has no effect.Beta Was this translation helpful? Give feedback.
All reactions