MCPcopy
hub / github.com/TanStack/router / handleServerRoutes

Function handleServerRoutes

packages/start-server-core/src/createStartHandler.ts:706–797  ·  view source on GitHub ↗
({
  getRouter,
  request,
  url,
  executeRouter,
  context,
  executedRequestMiddlewares,
}: {
  getRouter: () => Promise<AnyRouter>
  request: Request
  url: URL
  executeRouter: (
    serverContext: any,
    matchedRoutes?: ReadonlyArray<AnyRoute>,
  ) => Promise<Response>
  context: any
  executedRequestMiddlewares: Set<AnyRequestMiddleware>
})

Source from the content-addressed store, hash-verified

704}
705
706async function handleServerRoutes({
707 getRouter,
708 request,
709 url,
710 executeRouter,
711 context,
712 executedRequestMiddlewares,
713}: {
714 getRouter: () => Promise<AnyRouter>
715 request: Request
716 url: URL
717 executeRouter: (
718 serverContext: any,
719 matchedRoutes?: ReadonlyArray<AnyRoute>,
720 ) => Promise<Response>
721 context: any
722 executedRequestMiddlewares: Set<AnyRequestMiddleware>
723}): Promise<Response> {
724 const router = await getRouter()
725 const rewrittenUrl = executeRewriteInput(router.rewrite, url)
726 const pathname = rewrittenUrl.pathname
727 // this will perform a fuzzy match, however for server routes we need an exact match
728 // if the route is not an exact match, executeRouter will handle rendering the app router
729 // the match will be cached internally, so no extra work is done during the app router render
730 const { matchedRoutes, foundRoute, routeParams } =
731 router.getMatchedRoutes(pathname)
732
733 const isExactMatch = foundRoute && routeParams['**'] === undefined
734
735 // Collect and dedupe route middlewares
736 const routeMiddlewares: Array<AnyMiddlewareServerFn> = []
737
738 // Collect middleware from matched routes, filtering out those already executed
739 // in the request phase
740 for (const route of matchedRoutes) {
741 const serverMiddleware = route.options.server?.middleware as
742 | Array<AnyRequestMiddleware>
743 | undefined
744 if (serverMiddleware) {
745 const flattened = flattenMiddlewares(serverMiddleware)
746 for (const m of flattened) {
747 if (!executedRequestMiddlewares.has(m)) {
748 routeMiddlewares.push(m.options.server)
749 }
750 }
751 }
752 }
753
754 // Add handler middleware if exact match
755 const server = foundRoute?.options.server
756 if (server?.handlers && isExactMatch) {
757 const handlers =
758 typeof server.handlers === 'function'
759 ? server.handlers({ createHandlers: (d: any) => d })
760 : server.handlers
761
762 const requestMethod = request.method.toUpperCase() as RouteMethod
763 const handler = handlers[requestMethod] ?? handlers['ANY']

Callers 1

requestHandlerMiddlewareFunction · 0.85

Calls 8

executeRewriteInputFunction · 0.90
flattenMiddlewaresFunction · 0.90
handlerToMiddlewareFunction · 0.85
executeRouterFunction · 0.85
toUpperCaseMethod · 0.80
getRouterFunction · 0.70
executeMiddlewareFunction · 0.70
hasMethod · 0.45

Tested by

no test coverage detected