Skip to content

Commit

Permalink
fix: layout route matching
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jul 9, 2023
1 parent 131aff6 commit 8bc9a2c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ export class Router<
routes: AnyRoute[],
layoutRoutes: AnyRoute[] = [],
): undefined | Route => {
let found: undefined | Route
let foundRoute: undefined | Route
let foundParams: undefined | AnyPathParams
// Given a list of routes, find the first route that matches
routes.some((route) => {
const children = route.children as undefined | Route[]
Expand All @@ -619,7 +620,8 @@ export class Router<
// If we found a child match, mark it as found
// and return true to stop the loop
if (childMatch) {
found = childMatch
foundRoute = childMatch
foundParams = undefined
return true
}
return false
Expand Down Expand Up @@ -649,12 +651,8 @@ export class Router<
}
}

matchingRoutesAndParams.push(
...layoutRoutes.map((d) => ({ route: d })),
{ route, params: parsedParams },
)

found = route
foundRoute = route
foundParams = parsedParams
return true
}

Expand All @@ -663,21 +661,28 @@ export class Router<

// If we didn't find a match in this route branch
// return early.
if (!found) {
if (!foundRoute) {
return undefined
}

matchingRoutesAndParams.push(...layoutRoutes.map((d) => ({ route: d })), {
route: foundRoute,
params: foundParams,
})

// If the found route has children, recurse again
const foundChildren = found.children as any
const foundChildren = foundRoute.children as any
if (foundChildren?.length) {
return findRoutes(foundChildren)
}

return found
return foundRoute
}

findRoutes([this.routeTree as any])

console.log(matchingRoutesAndParams.map((d) => d.route.id))

// Alright, by now we should have all of our
// matching routes and their param pairs, let's
// Turn them into actual `Match` objects and
Expand Down

0 comments on commit 8bc9a2c

Please sign in to comment.