-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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(hmr): hmr websocket failure for custom middleware mode with server.hmr.server #11487
fix(hmr): hmr websocket failure for custom middleware mode with server.hmr.server #11487
Conversation
2d4a5d9
to
b3bd80a
Compare
a46895c
to
94ef1cd
Compare
94ef1cd
to
1911e03
Compare
Thanks for the PR! I guess this will break in the case below.
https://stackblitz.com/edit/github-yckrb2?file=src%2Fmain.ts That said, I think this is a reasonable change as we suggest using About the tests, we have setup related tests here. These files might be helpful for writing tests there. |
/ecosystem-ci run |
📝 Ran ecosystem CI: Open
|
@sapphi-red, thanks for great suggestions! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Quoting for visibility to other reviewers:
I guess this will break in the case ... That said, I think this is a reasonable change
#11487 (comment)
Hello Vite team, thanks for maintaining this nice tool! I just found a bug so I would like to fix it
Description
When
server.middlewareMode: true
andappType: "custom"
, we can pass Node.js server instance toserver.hmr.server
option as well to make sure we use single port to serve everything (HTML, client script, and HMR WebSocket).In this case, currently the HMR WebSocket client tries to connect to the hard-coded port (
24678
), which is not listened on by anything and simply causes connection error.Please try a repro here to reproduce the bug. (UPDATE: Now I included new playground & test case in this PR so you can use them instead as a repro)
In this PR, I propose to make sure to use
importMetaUrl.port
under this configuration so that the HMR WebSocket client can always connect to the server reliably in any case even under HTTP reverse proxy with WebSocket support, also under a firewall to allow only a single port (e.g.8080
port for everything)I now also added
playground/custom-server-single-port
to easily confirm the fixed behavior with a test case to verify established WebSocket connection with the correct target URL.About the importance of this bug, this setup is quite useful when the dev server can be mounted in different environments including reverse proxies (assuming the reverse proxy can proxy WebSocket as well). Making sure multiple ports works fine with any environment is hard considering there can be many intermediate things including firewall / proxy config.
Additional context
I have several things I need help from reviewers.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).Notes on the added test case in this PR
I confirmed that this added test case fails as expected on the current
main
branch (13ac37d) and it passes with this PR.Here is the local test run log:
On
main
(13ac37d)Test run output
This PR
Test run output