Skip to content

Commit a415471

Browse files
authored
perf(router-core): cache lightweight route matches (#7601)
1 parent 854ac95 commit a415471

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

‎packages/router-core/src/router.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,18 @@ export const locationHistoryActions = new WeakMap<
909909
HistoryAction
910910
>()
911911

912+
type LightweightRouteMatchResult = {
913+
matchedRoutes: ReadonlyArray<AnyRoute>
914+
fullPath: string
915+
search: Record<string, unknown>
916+
params: Record<string, unknown>
917+
}
918+
919+
type LightweightRouteMatchCacheEntry = [
920+
lastMatchId: string | undefined,
921+
result: LightweightRouteMatchResult,
922+
]
923+
912924
export type CreateRouterFn = <
913925
TRouteTree extends AnyRoute,
914926
TTrailingSlashOption extends TrailingSlashOption = 'never',
@@ -1002,6 +1014,10 @@ export class RouterCore<
10021014
processedTree!: ProcessedTree<TRouteTree, any, any>
10031015
resolvePathCache!: LRUCache<string, string>
10041016
private routeBranchCache = new WeakMap<AnyRoute, ReadonlyArray<AnyRoute>>()
1017+
private lightweightCache = new WeakMap<
1018+
ParsedLocation,
1019+
LightweightRouteMatchCacheEntry
1020+
>()
10051021
isServer!: boolean
10061022
pathParamsDecoder?: (encoded: string) => string
10071023
protocolAllowlist!: Set<string>
@@ -1719,12 +1735,15 @@ export class RouterCore<
17191735
* Only computes fullPath, accumulated search, and params - skipping expensive
17201736
* operations like AbortController, ControlledPromise, loaderDeps, and full match objects.
17211737
*/
1722-
private matchRoutesLightweight(location: ParsedLocation): {
1723-
matchedRoutes: ReadonlyArray<AnyRoute>
1724-
fullPath: string
1725-
search: Record<string, unknown>
1726-
params: Record<string, unknown>
1727-
} {
1738+
private matchRoutesLightweight(
1739+
location: ParsedLocation,
1740+
): LightweightRouteMatchResult {
1741+
const lastStateMatchId = last(this.stores.matchesId.get())
1742+
const cached = this.lightweightCache.get(location)
1743+
if (cached && cached[0] === lastStateMatchId) {
1744+
return cached[1]
1745+
}
1746+
17281747
const { matchedRoutes, routeParams } = this.getMatchedRoutes(
17291748
location.pathname,
17301749
)
@@ -1753,7 +1772,6 @@ export class RouterCore<
17531772
}
17541773

17551774
// Determine params: reuse from state if possible, otherwise parse
1756-
const lastStateMatchId = last(this.stores.matchesId.get())
17571775
const lastStateMatch =
17581776
lastStateMatchId && this.stores.matchStores.get(lastStateMatchId)?.get()
17591777
const canReuseParams =
@@ -1780,12 +1798,14 @@ export class RouterCore<
17801798
params = strictParams
17811799
}
17821800

1783-
return {
1801+
const result = {
17841802
matchedRoutes,
17851803
fullPath: lastRoute.fullPath,
17861804
search: accumulatedSearch,
17871805
params,
17881806
}
1807+
this.lightweightCache.set(location, [lastStateMatchId, result])
1808+
return result
17891809
}
17901810

17911811
cancelMatch = (id: string) => {

0 commit comments

Comments
 (0)