fix(routing): use serializeHandlerFn for routed middleware - #4563
fix(routing): use serializeHandlerFn for routed middleware#4563koding88 wants to merge 2 commits into
Conversation
|
@koding88 is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughThe routing template now serializes routed middleware as direct handler references. The unit test remains within the virtual routing template test group and verifies the expected ChangesRouted middleware serialization
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This change makes route-scoped middleware executable at request time instead of passing route metadata as a middleware function, addressing the reported HTTP 500 failure. No actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The change addresses issue
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Fixes a runtime failure when using route-scoped middleware (middleware: true + route pattern) by ensuring routed middleware is serialized as executable handler functions (via serializeHandlerFn) rather than route-record objects, aligning the generated #nitro/virtual/routing output with how #nitro/virtual/app consumes findRoutedMiddleware(...).map(r => r.data).
Changes:
- Switch
findRoutedMiddlewarecompilation to useserializeHandlerFnsor.datais a callable middleware handler. - Add a unit test ensuring routed middleware serialization emits
h3.toEventHandler(...)rather than a{ route, method, meta, handler }record.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/build/virtual/routing.ts |
Adjusts routed-middleware serialization so generated middleware entries are executable handler functions. |
test/unit/virtual-routing.test.ts |
Adds regression coverage asserting the routed-middleware serializer output uses serializeHandlerFn semantics. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| it("serializes routed middleware with serializeHandlerFn instead of route record", () => { | ||
| const handler: NitroEventHandler & { _importHash: string } = { | ||
| route: "/api/**", | ||
| handler: "/path/to/middleware.ts", | ||
| _importHash: "_mid123", | ||
| middleware: true, | ||
| }; | ||
| const nitroStub = { | ||
| options: { baseURL: "/", routeRules: {} }, | ||
| routing: { | ||
| routes: { routes: [], compileToString: () => "{}" }, | ||
| routedMiddleware: { | ||
| routes: [{ route: "/api/**", data: handler }], | ||
| compileToString: ({ serialize }: { serialize: (h: unknown) => string }) => | ||
| `{"/api/**":${serialize(handler)}}`, | ||
| }, | ||
| globalMiddleware: [], | ||
| }, | ||
| } as unknown as Nitro; | ||
| const template = routing(nitroStub).template(); | ||
| expect(template).toContain( | ||
| 'export const findRoutedMiddleware = {"/api/**":h3.toEventHandler(_mid123)};' | ||
| ); | ||
| }); |
Summary
Fixes #4557
When registering a handler with
middleware: trueand a route pattern (route-scoped middleware),findRoutedMiddlewarecompiled withserialize: serializeHandler, which emits route record objects{ route, method, meta, handler }as route data.In the generated
#nitro/virtual/apptemplate:the route record object was passed directly to H3 as a middleware function, causing runtime requests to fail with
TypeError: fn is not a function(HTTP 500).Background
Global middleware in
src/build/virtual/routing.tsis serialized viaserializeHandlerFn, emitting bare handler functions (wrapped inh3.toEventHandler). Route-scoped middleware infindRoutedMiddlewareshould also be serialized as handler functions sor.datayields executable middleware handlers for H3.Verification
test/unit/virtual-routing.test.tsverifyingfindRoutedMiddlewarecompiles withserializeHandlerFn.pnpm test:rolldownandpnpm test:rollup(55 test files, 923 tests passed).pnpm fmt,pnpm lint,pnpm typecheck).