Skip to content

fix(vite-dev-server): use Vite's error handler for Service Worker requests - #58

Merged
kazupon merged 2 commits into
mainfrom
fix/sw-error-handler
Oct 1, 2026
Merged

kazupon merged 2 commits into
mainfrom
fix/sw-error-handler

Conversation

@kazupon

@kazupon kazupon commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Closes #56

The Service Worker never used Vite's errorMiddleware, and the handler would have thrown if it had run.

  • createServer() passed the original Hono app to handle(), but listen() later replaced middlewares with middlewares.basePath(basePath) and registered onError() on that clone. basePath() returns a clone with its own error handler. With a non-root base path, which Vrowzer always uses, Hono's default handler therefore answered with a plain-text Internal Server Error 500, and nothing was logged through the Vite logger.
  • errorMiddleware calls logError(), which sends an HMR error payload through server.environments.client.hot.send(). The Service Worker-side server object has no environments, so the handler throws whenever it runs, and the request fails with a network error. Today this happens only with basePath /, the default of createServer().

Fixing only the registration would turn Vrowzer's 500 responses into network errors, so this PR fixes both, in this order:

  1. logError() skips the HMR payload when environments is absent. The upstream lines are kept as comments with a NOTE(kazupon).
  2. createServer() applies the base path before handle(), so routes and onError() are registered on the app that handles requests, and the swap in listen() is removed. No middleware was registered before the old swap, so route paths do not change.

Each new test failed before its fix:

  • error.test.ts: errorMiddleware with a server that has no environments returns the 500 HTML response and logs the error. Before the fix it failed with TypeError: Cannot read properties of undefined (reading 'client').
  • vrowzer.integration-test.ts: a preview request for a .ts module with a syntax error returns Vite's error page with status 500, and the page names the module. Before the fix the body was the plain-text Internal Server Error.

Visible changes:

  • When index.html fails to load, the preview now receives Vite's error page instead of the text Internal Server Error.
  • The existing console.log('[error] viteErrorHandler called for:', ...) in errorMiddleware now runs for failed requests.

Out of scope: the Service Worker still does not deliver the HMR error payload, so the error overlay does not appear. Errors from the Web Worker also lose loc and frame, so the error page shows no code frame.

Validated locally with vp run test:unit (116 files, 1,841 tests), vp run test:integration:vrowzer (44 passed, 1 conditional skip), vp run test:e2e (46 passed, 6 existing test.skip), oxlint (no new warnings), the format check of the changed files, and typecheck.

…e absent

logError() sends an HMR error payload through server.environments.client.hot.send(), but the Service Worker-side server object has no environments. The error handler therefore throws, and the request fails with a network error instead of returning the 500 error page. Skip the payload when environments is absent. The Web Worker-side server is unaffected.
…e app that handles requests

createServer() passed the original Hono app to handle(), but listen() later replaced middlewares with middlewares.basePath(basePath) and registered onError() on that clone. basePath() returns a clone with its own error handler, so with a non-root base path, which Vrowzer always uses, Hono's default handler answered with a plain-text 500 and errorMiddleware never ran. Apply the base path before handle() so that the routes and the error handler are registered on the app that handles requests. The new integration test requests a module with a syntax error and expects Vite's error page.
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