ITADN

RedirectMessage rethrown before headersSent check crashes Vite dev middleware (router 2.0-beta.32)

#8565Closedlapinponpin 创建于 2026-04-21
bugrouterV2
L
lapinponpincommented
## Summary In `@qwik.dev/router@2.0.0-beta.32`, the node middleware rethrows `RedirectMessage` (an `AbortMessage` subclass) before checking `headersSent`. `RedirectMessage` has no `.message`/`.stack`, so when it propagates to Vite's `viteErrorMiddleware`, `stripVTControlCharacters(undefined)` crashes with `TypeError [ERR_INVALID_ARG_TYPE]`. ## Repro Any `throw redirect(302, '/foo')` in a `routeAction$` or `onRequest$` during `vite --mode ssr`. Example: a successful POST to `/auth/login/` that redirects on success. ## Expected Qwik already sent the 302 response (stream closed) — the middleware should return cleanly, not forward the `RedirectMessage` to `next(err)`. ## Actual ``` RedirectMessage {} [vite] Internal server error: undefined TypeError [ERR_INVALID_ARG_TYPE]: The "str" argument must be of type string. Received undefined at stripVTControlCharacters (node:internal/util/inspect:2765:3) at prepareError (vite/dist/node/chunks/config.js:9475:12) at logError (vite/dist/node/chunks/config.js:9504:8) at viteErrorMiddleware (vite/dist/node/chunks/config.js:9509:3) ... at router (@qwik.dev/router/lib/middleware/node/index.mjs:162:7) ``` ## Root cause `@qwik.dev/router/lib/middleware/node/index.mjs` around line 142: ```js const handled = await requestHandler(serverRequestEv, opts); if (handled) { const err = await handled.completion; if (err) { throw err; // fires for RedirectMessage } if (handled.requestEv.headersSent) { return; // never reached } } ``` `runOnce()` (in `request-handler/index.mjs` around line 1554) already does `await stream.close()` for `RedirectMessage` before returning it, so the response is complete — the rethrow is spurious. ## Suggested fix Check `headersSent` first, or short-circuit on `RedirectMessage`/`AbortMessage`: ```js if (handled) { const err = await handled.completion; if (handled.requestEv.headersSent) return; if (err) throw err; } ``` ## Environment - \`@qwik.dev/router\` 2.0.0-beta.32 - \`@qwik.dev/core\` 2.0.0-beta.32 - Vite 7.3.1 - Node 22 LTS - Platform: macOS (darwin 25.4.0)
关闭于 2026-05-19 1 条评论