-
Notifications
You must be signed in to change notification settings - Fork 113
fix(dev): set hmr server on server.ws (vite 8.1+)
#1355
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
Merged
+72
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { Server } from 'node:http' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { attachViteHmrServer } from '../../src/dev/utils' | ||
|
|
||
| const hmrServer = {} as Server | ||
|
|
||
| describe('attachViteHmrServer', () => { | ||
| it('pins the dev server on both ws and hmr with no separate port', () => { | ||
| const server: Record<string, unknown> = {} | ||
|
|
||
| attachViteHmrServer(server, hmrServer) | ||
|
|
||
| for (const key of ['ws', 'hmr'] as const) { | ||
| expect(server[key]).toMatchObject({ | ||
| protocol: undefined, | ||
| port: undefined, | ||
| host: undefined, | ||
| server: hmrServer, | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| it('preserves user-set ws and hmr options other than server/port/host', () => { | ||
| const server: Record<string, unknown> = { | ||
| ws: { clientPort: 1234 }, | ||
| hmr: { overlay: false }, | ||
| } | ||
|
|
||
| attachViteHmrServer(server, hmrServer) | ||
|
|
||
| expect(server.ws).toMatchObject({ clientPort: 1234, server: hmrServer, port: undefined, host: undefined }) | ||
| expect(server.hmr).toMatchObject({ overlay: false, server: hmrServer, port: undefined, host: undefined }) | ||
| }) | ||
|
|
||
| it('does not crash when ws or hmr is set to false', () => { | ||
| const server: Record<string, unknown> = { ws: false, hmr: false } | ||
|
|
||
| expect(() => attachViteHmrServer(server, hmrServer)).not.toThrow() | ||
|
|
||
| expect(server.ws).toMatchObject({ server: hmrServer }) | ||
| expect(server.hmr).toMatchObject({ server: hmrServer }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🎯 Functional Correctness | 🟡 Minor
🧩 Analysis chain
🌐 Web query:
In Vite 8.1+, what do server.hmr.protocol, server.hmr.port and server.hmr.host control, and is protocol inferred from server.hmr.server when omitted?💡 Result:
In Vite 8.1+, the configuration options previously located under
server.hmr(specificallyprotocol,host,port,path,clientPort,timeout, andserver) have been deprecated and moved toserver.ws[1][2][3]. These options are automatically synced, so existingserver.hmrconfigurations will continue to function [1][2]. The options serve the following purposes: protocol: Specifies the WebSocket protocol used for the connection, typically ws or wss [1][2]. host: Defines the host for the WebSocket server [1][2]. port: Sets the port for the WebSocket server [1][2]. server: Allows providing a custom HTTP server for WebSocket connections, which is useful when using self-signed certificates or when you need to expose the HMR WebSocket and HTTP traffic over a single port [1][2]. Regarding your question on protocol inference: Whenserver.ws.server(or the deprecatedserver.hmr.server) is defined, Vite processes WebSocket connection requests through the provided custom server [1][2]. If no protocol is explicitly specified, Vite does not automatically infer the protocol strictly from theserverobject itself; rather, the protocol is typically determined based on the server's configuration (e.g., whether it is an HTTPS server) or defaults [1][2]. Using a custom server allows Vite to handle HMR connections through that server instance, maintaining consistent security and network handling [1][2].Citations:
server.hmroptions toserver.wsoptions vitejs/vite#21357protocoloverride logic is incorrect inattachViteHmrServer.Placing
protocol: undefinedbefore the spread...allows user-provided values to override it, unlikeportandhostwhich are correctly placed after the spread to force clearing. To consistently clear the protocol (e.g., to let Vite infer it from the attachedhmrServeror default), ensureprotocolis defined after the spread.Proposed fix
target.ws = { - protocol: undefined, ...(target.ws as HmrOptions), + protocol: undefined, port: undefined, host: undefined, server: hmrServer, } target.hmr = { - protocol: undefined, ...(target.hmr as HmrOptions), + protocol: undefined, port: undefined, host: undefined, server: hmrServer, }Additionally, this function silently overwrites explicit
ws: falseorhmr: falseconfigurations with an object, effectively re-enabling HMR. Verify if this override behavior is intended.📝 Committable suggestion
🤖 Prompt for AI Agents