Skip to content

Commit 6937940

Browse files
committed
perf(router): preserve synchronous inline handlers
1 parent 3362bb9 commit 6937940

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

storage/framework/core/router/src/stacks-router.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,8 @@ function createMiddlewareHandler(routeKey: string, handler: StacksHandler): Rout
18711871
// `let` (not `const`) because the post-action CORS wrapper below may
18721872
// replace it with a header-mutated copy when the original Response's
18731873
// Headers are immutable.
1874-
let response = await wrappedBase(enhancedReq)
1874+
const baseResult = wrappedBase(enhancedReq)
1875+
let response = baseResult instanceof Response ? baseResult : await baseResult
18751876

18761877
// Clear tracked queries after each request to prevent accumulation
18771878
clearTrackedQueries()
@@ -3614,10 +3615,12 @@ function wrapHandler(handler: StacksHandler, skipParsing = false, handlerKey = '
36143615
// primitive → JSON-encoded scalar, same Response passthrough.
36153616
// Without this wrapper, function handlers would return strings to
36163617
// bun-router and end up as `text/plain` regardless of the client.
3617-
const fn = handler as RouteHandlerFn
3618-
return async (req: EnhancedRequest) => {
3619-
const result = await fn(req)
3620-
return formatResult(result as unknown, req)
3618+
const fn = handler as InlineRouteHandler
3619+
return (req: EnhancedRequest) => {
3620+
const result = fn(req)
3621+
if (result instanceof Promise)
3622+
return result.then(value => formatResult(value, req))
3623+
return formatResult(result, req)
36213624
}
36223625
}
36233626

0 commit comments

Comments
 (0)