Repository navigation
fix(vite-dev-server): use Vite's error handler for Service Worker requests - #58
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 tohandle(), butlisten()later replacedmiddlewareswithmiddlewares.basePath(basePath)and registeredonError()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-textInternal Server Error500, and nothing was logged through the Vite logger.errorMiddlewarecallslogError(), which sends an HMR error payload throughserver.environments.client.hot.send(). The Service Worker-side server object has noenvironments, so the handler throws whenever it runs, and the request fails with a network error. Today this happens only withbasePath/, the default ofcreateServer().Fixing only the registration would turn Vrowzer's 500 responses into network errors, so this PR fixes both, in this order:
logError()skips the HMR payload whenenvironmentsis absent. The upstream lines are kept as comments with aNOTE(kazupon).createServer()applies the base path beforehandle(), so routes andonError()are registered on the app that handles requests, and the swap inlisten()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:errorMiddlewarewith a server that has noenvironmentsreturns the 500 HTML response and logs the error. Before the fix it failed withTypeError: Cannot read properties of undefined (reading 'client').vrowzer.integration-test.ts: a preview request for a.tsmodule 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-textInternal Server Error.Visible changes:
index.htmlfails to load, the preview now receives Vite's error page instead of the textInternal Server Error.console.log('[error] viteErrorHandler called for:', ...)inerrorMiddlewarenow 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
locandframe, 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 existingtest.skip), oxlint (no new warnings), the format check of the changed files, and typecheck.