Skip to content

fix(vite-dev-server): handle missing environments in error middleware - #57

Open
Fire162 wants to merge 1 commit into
kazupon:mainfrom
Fire162:fix/sw-error-middleware
Open

Fire162 wants to merge 1 commit into
kazupon:mainfrom
Fire162:fix/sw-error-middleware

Conversation

@Fire162

@Fire162 Fire162 commented Oct 1, 2026

Copy link
Copy Markdown

Summary

Fixes #56.

In the Service Worker environment (ViteDevServerForServiceWorker), server.environments is undefined because DevEnvironment instances are not created.

When middleware throws an error, errorMiddleware invokes logError(server, err), which attempted to access server.environments.client.hot.send(...). This caused an unhandled TypeError: Cannot read properties of undefined (reading 'client') inside Hono's onError handler, resulting in a rejected FetchEvent.respondWith() promise and producing a browser network error instead of the intended 500 error response.

Changes

  • In packages/vite-dev-server/src/node/server/middlewares/error.ts, safely guard server.environments?.client?.hot?.send(...) with optional chaining when client environments or hot channels are absent.
  • In packages/vite-dev-server/src/node/server/middlewares/error.test.ts, add a unit test asserting that errorMiddleware returns a 500 status and renders error HTML when environments is undefined on the server instance.

This branch has not been deployed

No deployments
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.

Use Vite's error handler for failed Service Worker requests

1 participant