Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions packages/nuxi/src/dev/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import type { Nuxt, NuxtConfig, ViteConfig } from '@nuxt/schema'
import type { DotenvOptions } from 'c12'
import type { Listener, ListenOptions } from 'listhen'
import type { createDevServer } from 'nitro/builder'
import type { NitroDevServer } from 'nitropack'
import type { FSWatcher } from 'node:fs'
import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http'
import type { Server as HttpServer, IncomingMessage, RequestListener, ServerResponse } from 'node:http'

import EventEmitter from 'node:events'
import { existsSync, readdirSync, statSync, watch } from 'node:fs'
Expand Down Expand Up @@ -116,6 +116,31 @@ export class FileChangeTracker {

type NuxtWithServer = Omit<Nuxt, 'server'> & { server?: NitroDevServer | ReturnType<typeof createDevServer> }

type ViteServerOptions = NonNullable<ViteConfig['server']>
type HmrOptions = Exclude<ViteServerOptions['hmr'], boolean>

/**
* Pin Vite's HMR websocket to the main dev server so no separate HMR port is allocated.
* vite >= 8.1 reads `server.ws`; older versions only read `server.hmr`.
*/
export function attachViteHmrServer(server: ViteServerOptions, hmrServer: HttpServer): void {
const target = server as Omit<ViteServerOptions, 'ws'> & { ws?: HmrOptions | boolean }
target.ws = {
protocol: undefined,
...(target.ws as HmrOptions),
port: undefined,
host: undefined,
server: hmrServer,
}
target.hmr = {
protocol: undefined,
...(target.hmr as HmrOptions),
port: undefined,
host: undefined,
server: hmrServer,
}
}
Comment on lines +126 to +142

Copy link
Copy Markdown

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 (specifically protocol, host, port, path, clientPort, timeout, and server) have been deprecated and moved to server.ws [1][2][3]. These options are automatically synced, so existing server.hmr configurations 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: When server.ws.server (or the deprecated server.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 the server object 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:


protocol override logic is incorrect in attachViteHmrServer.

Placing protocol: undefined before the spread ... allows user-provided values to override it, unlike port and host which are correctly placed after the spread to force clearing. To consistently clear the protocol (e.g., to let Vite infer it from the attached hmrServer or default), ensure protocol is 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: false or hmr: false configurations with an object, effectively re-enabling HMR. Verify if this override behavior is intended.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function attachViteHmrServer(server: ViteServerOptions, hmrServer: HttpServer): void {
const target = server as Omit<ViteServerOptions, 'ws'> & { ws?: HmrOptions | boolean }
target.ws = {
protocol: undefined,
...(target.ws as HmrOptions),
port: undefined,
host: undefined,
server: hmrServer,
}
target.hmr = {
protocol: undefined,
...(target.hmr as HmrOptions),
port: undefined,
host: undefined,
server: hmrServer,
}
}
export function attachViteHmrServer(server: ViteServerOptions, hmrServer: HttpServer): void {
const target = server as Omit<ViteServerOptions, 'ws'> & { ws?: HmrOptions | boolean }
target.ws = {
...(target.ws as HmrOptions),
protocol: undefined,
port: undefined,
host: undefined,
server: hmrServer,
}
target.hmr = {
...(target.hmr as HmrOptions),
protocol: undefined,
port: undefined,
host: undefined,
server: hmrServer,
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/nuxi/src/dev/utils.ts` around lines 126 - 142, The HMR attachment
logic in attachViteHmrServer is clearing protocol in the wrong order, so
user-specified ws/hmr protocol values can override the intended reset. Update
the target.ws and target.hmr assignments so protocol is forced after the spread,
matching the handling used for port and host, and keep the existing hmrServer
attachment. Also review the current behavior in attachViteHmrServer for explicit
false values on ws or hmr, since the function now converts them into objects and
re-enables HMR unexpectedly.


interface DevServerEventMap {
'loading:error': [error: Error]
'loading': [loadingMessage: string]
Expand Down Expand Up @@ -349,13 +374,7 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
if (!process.env.NUXI_DISABLE_VITE_HMR) {
this.#currentNuxt.hooks.hook('vite:extend', ({ config }) => {
if (config.server) {
config.server.hmr = {
protocol: undefined,
...(config.server.hmr as Exclude<typeof config.server.hmr, boolean>),
port: undefined,
host: undefined,
server: this.listener.server,
}
attachViteHmrServer(config.server, this.listener.server)
}
})
}
Expand Down
44 changes: 44 additions & 0 deletions packages/nuxi/test/unit/dev-hmr.spec.ts
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 })
})
})
Loading