Vite dev server plugin transform hangs after client disconnect
0 reputation · 16 Apr 2022, 11:33 UTC
0 reputation · 16 Apr 2022, 11:33 UTC
Goal: diagnose whether Vite aborts in-flight plugin load or transform work when a browser request is cancelled or the dev server reloads. Vite routes dev requests through async plugin hooks, yet the plugin API does not pass an AbortSignal or cancellation token to those hooks.
A hanging transform can keep its HTTP request open until Node.js or the client times out. HMR reconnects on the client, but server-side cleanup of stale client subscriptions or timed-out pending updates is not explicitly configurable. Whether server.close() interrupts active plugin work or awaits it is undocumented.
Assumes a current Vite dev server release; verify the installed version before relying on this behavior. A practical check is to inspect the plugin API reference for cancellation parameters and observe a slow transform after an aborted page load.
Does Vite cancel an in-flight transform when the requesting client disconnects? Does server.close() await active plugin hooks or terminate them immediately? Is there a supported way to enforce a per-request deadline for dev server transforms?
No. Vite does not cancel an in-flight load or transform hook when the browser disconnects or cancels the request. server.close() is graceful: it stops accepting new connections and waits for existing connections to end, but it does not document that it interrupts or kills active plugin hooks. There is no built-in, supported per-request deadline for dev-server transforms.
Vite plugin hooks follow the Rollup plugin contract. Those hooks receive the source, an options object, and a plugin context, but not an AbortSignal or cancellation token. When a client cancels a fetch, Node.js may emit request-level close or abort events, but Vite does not forward those signals into the transform pipeline. The transform therefore continues until it completes, throws, or the Node.js process exits.
A hung transform can also block later requests for the same resolved module ID because Vite caches successful transform results by module ID. Other modules are usually unaffected.
load and transform hooks do not accept an AbortSignal parameter in the published plugin API.server.close() stops new connections and invokes its callback after existing connections end; it is not documented as an immediate termination of active hooks.npx vite --version. Behavior differs across major versions, so record this before applying version-specific fixes.server.close() while a slow transform runs and log whether the close callback fires before or after the transform completes.AbortSignal or signal on load and transform to confirm whether your version added cancellation support.If a specific transform is hanging, scope any timeout wrapper to that transform only. Do not apply a global request timeout to Vite's dev server middleware without testing HMR and the transform cache, because a poorly scoped timeout can leave partial cache entries or interrupt legitimate long-running transforms.
This answer assumes a current Vite dev server release and the standard Rollup-derived plugin API. It does not cover custom server middleware or third-party plugins that implement their own request handling. The exact callback timing of server.close() depends on open WebSocket/HMR connections and keep-alive sockets, not just plugin transforms, so treat the close behavior as version-dependent until you verify it with the steps above.
Missing diagnostic detail: run npx vite --version and share the major version. If you are on an older major version, inspecting type definitions for AbortSignal becomes more important because newer versions may have changed hook signatures.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 16 Apr 2022, 13:29 UTC
When a browser cancels a Vite dev request, the HTTP connection closes but plugin transform promises may remain scheduled on the Node event loop since the hooks receive no AbortSignal. Plugins must explicitly race against a timeout to prevent lingering work. Verify the installed Vite version (`vite/package.json`) and test with a slow transform followed by a browser navigation abort to observe persistence.