Skip to content

fix(vite): skip websocket upgrade listener in middleware mode - #4647

Merged
pi0 merged 2 commits into
nitrojs:mainfrom
benjamincanac:fix/vite-ws-middleware-mode
Sep 22, 2026
Merged

pi0 merged 2 commits into
nitrojs:mainfrom
benjamincanac:fix/vite-ws-middleware-mode

Conversation

@benjamincanac

Copy link
Copy Markdown
Contributor

🔗 Linked issue

None.

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

With features.websocket enabled, configureViteDevServer calls server.httpServer!.on("upgrade"). In middleware mode Vite has no http server, so httpServer is null and the dev server crashes on startup:

ERROR  Cannot read properties of null (reading 'on')
    at configureViteDevServer (nitro/dist/_build/vite.dev.mjs:80:98)

This is what happens with the Nuxt 5 nightlies, which mount Nitro as a Vite environment and run Vite in middleware mode. Any app with nitro.features.websocket can't start nuxt dev.

The listener is now skipped when there is no httpServer, the same way the SIGINT handler below already leaves middleware mode alone. The embedding server owns the real listener there, and it can forward upgrades itself through server.environments.nitro.devServer.upgrade().

I applied the same guard to the installed 3.0.260903-beta in a Nuxt app with websockets on, and nuxt dev boots and serves pages again. WebSockets in dev still need Nuxt to forward upgrades, which is a separate change on the Nuxt side.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@benjamincanac
benjamincanac requested a review from pi0 as a code owner September 21, 2026 10:31
@vercel

vercel Bot commented Sep 21, 2026

Copy link
Copy Markdown

@benjamincanac is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 16874370-cc50-4dda-ba79-c4b761a513b7

📥 Commits

Reviewing files that changed from the base of the PR and between 53a3487 and 0091a77.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 3251c521-ed49-4ecc-9b5d-b0d30e9e7f92

📥 Commits

Reviewing files that changed from the base of the PR and between 96c8e78 and 53a3487.

📒 Files selected for processing (1)
  • src/build/vite/dev.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The Vite development server now registers the WebSocket upgrade listener only when httpServer exists. In middleware mode, registration is skipped and upgrade handling remains with the parent server.

Changes

Vite upgrade handling

Layer / File(s) Summary
Optional upgrade listener registration
src/build/vite/dev.ts
configureViteDevServer changed the upgrade listener registration from a non-null assertion to optional chaining. The comment documents middleware-mode behavior.

Priority: ➖ Normal

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description explains the middleware-mode startup crash, the WebSocket listener guard, and the resulting behavior. It is directly related to the changeset.
Title check ✅ Passed The title follows the Conventional Commits format with the valid fix(vite): prefix and clearly describes the middleware-mode WebSocket listener change.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4647

commit: 0091a77

@pi0 pi0 added the v3 label Sep 22, 2026
@pi0
pi0 merged commit b5c05d8 into nitrojs:main Sep 22, 2026
13 of 14 checks passed
Q16solver added a commit to Esposter/Esposter that referenced this pull request Oct 4, 2026
Nuxt 5 ships only as nightly packages, so `nuxt`, `@nuxt/kit`,
`@nuxt/schema` and `@nuxt/nitro-server` are aliases of their `5x` dist-tag,
and `nitro` of its own nightly — in the catalog and in `overrides:`, so
`@nuxt/nitro-server`'s dependency follows — because the newest Nitro beta
crashes `nuxt dev` with WebSockets on (nitrojs/nitro#4647, merged, not yet
released). One disabled Renovate rule holds every alias.

What the h3 1.x/2.x split forced goes:

- the `h3` override, the `h3 <2` Renovate cap and the catalog's 1.x range;
  `h3` moves to 2.x and the app stops declaring it, since its server imports
  `nuxt/server` and the tRPC module `nitro/h3`
- the `crossws` override: Nitro 3 depends on the 0.4 line itself, and the
  0.3 copy left belongs to h3 1.x under the modules still on it
- the UnoCSS trio's tilde and its `<66.10` rule: the inspector's `devframe`
  resolves its own h3 2.x
- `nitropack`, replaced by `nitro`

Nuxt 5 applies its Nitro 2 compatibility layer by itself to the modules
whose server code still imports h3 1.x, so no override forces an h3 major
in either direction. What it does not reach is a module's build-time code
and a module's own dependencies, which Nitro 3 resolves from the app's
root. So the branch carries, each with an `@TODO`:

- a patch to nuxt-security, whose `prerender:done` hook reads Nitro 2's
  build-time `nitro.storage` and fails every prerender
- `lru-cache`, `chokidar`, `remark-emoji` and `mocked-exports` as the app's
  dev dependencies, for the storage drivers, MDC plugin and bufferutil
  alias the server bundle imports

The app's manifest also exports its own `package.json`, so the MCP server
reads its version by package name: Nuxt 5's server project maps no `@@`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TUTQr3Wd3pfCwmkTuyzoYi
Q16solver added a commit to Esposter/Esposter that referenced this pull request Oct 4, 2026
Nuxt 5 ships only as nightly packages, so `nuxt`, `@nuxt/kit`,
`@nuxt/schema` and `@nuxt/nitro-server` are aliases of their `5x` dist-tag,
and `nitro` of its own nightly — in the catalog and in `overrides:`, so
`@nuxt/nitro-server`'s dependency follows — because the newest Nitro beta
crashes `nuxt dev` with WebSockets on (nitrojs/nitro#4647, merged, not yet
released). One disabled Renovate rule holds every alias.

What the h3 1.x/2.x split forced goes:

- the `h3` override, the `h3 <2` Renovate cap and the catalog's 1.x range;
  `h3` moves to 2.x and the app stops declaring it, since its server imports
  `nuxt/server` and the tRPC module `nitro/h3`
- the `crossws` override: Nitro 3 depends on the 0.4 line itself, and the
  0.3 copy left belongs to h3 1.x under the modules still on it
- the UnoCSS trio's tilde and its `<66.10` rule: the inspector's `devframe`
  resolves its own h3 2.x
- `nitropack`, replaced by `nitro`

Nuxt 5 applies its Nitro 2 compatibility layer by itself to the modules
whose server code still imports h3 1.x, so no override forces an h3 major
in either direction. What it does not reach is a module's build-time code
and a module's own dependencies, which Nitro 3 resolves from the app's
root. So the branch carries, each with an `@TODO`:

- a patch to nuxt-security, whose `prerender:done` hook reads Nitro 2's
  build-time `nitro.storage` and fails every prerender
- `lru-cache`, `chokidar`, `remark-emoji` and `mocked-exports` as the app's
  dev dependencies, for the storage drivers, MDC plugin and bufferutil
  alias the server bundle imports

The app's manifest also exports its own `package.json`, so the MCP server
reads its version by package name: Nuxt 5's server project maps no `@@`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TUTQr3Wd3pfCwmkTuyzoYi
Q16solver added a commit to Esposter/Esposter that referenced this pull request Oct 4, 2026
Nuxt 5 ships only as nightly packages, so `nuxt`, `@nuxt/kit`,
`@nuxt/schema` and `@nuxt/nitro-server` are aliases of their `5x` dist-tag,
and `nitro` of its own nightly — in the catalog and in `overrides:`, so
`@nuxt/nitro-server`'s dependency follows — because the newest Nitro beta
crashes `nuxt dev` with WebSockets on (nitrojs/nitro#4647, merged, not yet
released). One disabled Renovate rule holds every alias.

What the h3 1.x/2.x split forced goes:

- the `h3` override, the `h3 <2` Renovate cap and the catalog's 1.x range;
  `h3` moves to 2.x and the app stops declaring it, since its server imports
  `nuxt/server` and the tRPC module `nitro/h3`
- the `crossws` override: Nitro 3 depends on the 0.4 line itself, and the
  0.3 copy left belongs to h3 1.x under the modules still on it
- the UnoCSS trio's tilde and its `<66.10` rule: the inspector's `devframe`
  resolves its own h3 2.x
- `nitropack`, replaced by `nitro`

Nuxt 5 applies its Nitro 2 compatibility layer by itself to the modules
whose server code still imports h3 1.x, so no override forces an h3 major
in either direction. What it does not reach is a module's build-time code
and a module's own dependencies, which Nitro 3 resolves from the app's
root. So the branch carries, each with an `@TODO`:

- a patch to nuxt-security, whose `prerender:done` hook reads Nitro 2's
  build-time `nitro.storage` and fails every prerender
- `lru-cache`, `chokidar`, `remark-emoji` and `mocked-exports` as the app's
  dev dependencies, for the storage drivers, MDC plugin and bufferutil
  alias the server bundle imports

The app's manifest also exports its own `package.json`, so the MCP server
reads its version by package name: Nuxt 5's server project maps no `@@`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TUTQr3Wd3pfCwmkTuyzoYi
(cherry picked from commit 665d781)
@pi0x pi0x removed the v3 label Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants