-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: Fix route matching specificity #7272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/router-core': patch | ||
| --- | ||
|
|
||
| Fix route matching specificity so routes with the same number of static segments prefer the match whose static segment occurs earlier, including when competing with wildcard routes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
packages/router-core/tests/new-process-route-tree.bench.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { bench, describe } from 'vitest' | ||
| import { findRouteMatch, processRouteTree } from '../src/new-process-route-tree' | ||
|
|
||
| type BenchRoute = { | ||
| id: string | ||
| isRoot?: boolean | ||
| fullPath: string | ||
| path: string | ||
| children?: Array<BenchRoute> | ||
| } | ||
|
|
||
| const BENCH_OPTIONS = { | ||
| warmupIterations: 100, | ||
| warmupTime: 1_500, | ||
| time: 3_000, | ||
| } | ||
|
|
||
| let benchmarkSink = 0 | ||
|
|
||
| function makeRouteTree(routes: Array<string>): BenchRoute { | ||
| return { | ||
| id: '__root__', | ||
| isRoot: true, | ||
| fullPath: '/', | ||
| path: '/', | ||
| children: routes.map((route) => ({ | ||
| id: route, | ||
| fullPath: route, | ||
| path: route, | ||
| })), | ||
| } | ||
| } | ||
|
|
||
| const unitDerivedPatterns = [ | ||
| // Derived from the priority and edge-case tests in new-process-route-tree.test.ts. | ||
| '/{-$a}/b', | ||
| '/a/$', | ||
| '/a/{-$b}', | ||
| '/{-$a}/{-$b}/{-$c}/d/e', | ||
| '/{-$a}/{-$b}/c/d/{-$e}', | ||
| '/$a/$b/$c/d/e', | ||
| '/$a/$b/c/d/$e', | ||
| '/users/{-$org}/settings', | ||
| '/users/$id', | ||
| '/{-$other}/posts/new', | ||
| '/posts/$id', | ||
| '/a/{-$b}/', | ||
| '/a/{-$b}/$c', | ||
| '/foo/{-$p}.tsx', | ||
| '/foo/{-$p}/{-$x}.tsx', | ||
| '/file{$}', | ||
| '/{$}/c/file', | ||
| ] | ||
|
|
||
| const unitDerivedMatchPaths = [ | ||
| '/a/b', | ||
| '/a/c', | ||
| '/a/b/c', | ||
| '/a/b/c/d/e', | ||
| '/users/settings', | ||
| '/posts/new', | ||
| '/foo/bar.tsx', | ||
| '/file/a/b/c', | ||
| '/x/y/c/file', | ||
| ] | ||
|
|
||
| function prefixed(pattern: string, index: number) { | ||
| return `/g${index}${pattern}` | ||
| } | ||
|
|
||
| function makeBigRoutes(groupCount: number) { | ||
| const routes: Array<string> = [] | ||
| for (let i = 0; i < groupCount; i++) { | ||
| for (const pattern of unitDerivedPatterns) { | ||
| routes.push(prefixed(pattern, i)) | ||
| } | ||
| } | ||
| return routes | ||
| } | ||
|
|
||
| function makeBigMatchPaths(groupCount: number) { | ||
| const paths: Array<string> = [] | ||
| for (let i = 0; i < groupCount; i++) { | ||
| for (const path of unitDerivedMatchPaths) { | ||
| paths.push(`/g${i}${path}`) | ||
| } | ||
| } | ||
| return paths | ||
| } | ||
|
|
||
| const routeSuites = [ | ||
| { | ||
| name: 'small route tree', | ||
| routes: unitDerivedPatterns, | ||
| matchPaths: unitDerivedMatchPaths, | ||
| buildIterations: 3_000, | ||
| matchIterations: 3_000, | ||
| }, | ||
| { | ||
| name: 'big route tree', | ||
| routes: makeBigRoutes(128), | ||
| matchPaths: makeBigMatchPaths(128), | ||
| buildIterations: 20, | ||
| matchIterations: 20, | ||
| }, | ||
| ] | ||
|
|
||
| describe.each(routeSuites)( | ||
| 'new process route tree - $name', | ||
| ({ routes, matchPaths, buildIterations, matchIterations }) => { | ||
| const routeTree = makeRouteTree(routes) | ||
| const processedTree = processRouteTree(routeTree).processedTree | ||
|
|
||
| bench( | ||
| `build ${routes.length} routes x ${buildIterations}`, | ||
| () => { | ||
| for (let i = 0; i < buildIterations; i++) { | ||
| const result = processRouteTree(routeTree) | ||
| benchmarkSink ^= result.routesById[routes[0]!] ? 1 : 0 | ||
| } | ||
| }, | ||
| BENCH_OPTIONS, | ||
| ) | ||
|
|
||
| bench( | ||
| `match ${matchPaths.length} paths x ${matchIterations} (uncached)`, | ||
| () => { | ||
| let matches = 0 | ||
|
|
||
| for (let i = 0; i < matchIterations; i++) { | ||
| processedTree.matchCache.clear() | ||
|
|
||
| for (const path of matchPaths) { | ||
| if (findRouteMatch(path, processedTree)) matches++ | ||
| } | ||
| } | ||
|
|
||
| const expectedMatches = matchPaths.length * matchIterations | ||
| if (matches !== expectedMatches) { | ||
| throw new Error(`Expected ${expectedMatches} matches, got ${matches}`) | ||
| } | ||
|
|
||
| benchmarkSink ^= matches | ||
| }, | ||
| BENCH_OPTIONS, | ||
| ) | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In JavaScript, are bitwise left-shift counts masked modulo 32? What do1 << 31,1 << 32, and1 << 33evaluate to?💡 Result:
Yes, in JavaScript, bitwise left-shift (<<) counts are masked to the lowest 5 bits (modulo 32). The right operand is converted to a uint32 and then shiftCount = rNum % 32, so shifts >=32 wrap around: 32 becomes 0, 33 becomes 1, etc. 1 << 31 evaluates to -2147483648 (0x80000000 in two's complement signed 32-bit int). 1 << 32 evaluates to 1 (equivalent to 1 << 0). 1 << 33 evaluates to 2 (equivalent to 1 << 1).
Citations:
🏁 Script executed:
Repository: TanStack/router
Length of output: 2951
🏁 Script executed:
Repository: TanStack/router
Length of output: 444
🏁 Script executed:
Repository: TanStack/router
Length of output: 504
🏁 Script executed:
Repository: TanStack/router
Length of output: 1845
🏁 Script executed:
Repository: TanStack/router
Length of output: 1367
Fix off-by-one shift in
appendStaticMask: useindex - 1for 0-based bit positions.Line 1014 uses
1 << indexwith a 1-basedindex(starting at 1). In JavaScript, shift counts wrap modulo 32, so1 << 32equals1 << 0. This causes segment 32 to set bit 0, whichisFrameMoreSpecifictreats as the lowest priority bit, making static matches at segment 32 incorrectly rank higher than matches at segment 1.Suggested fix
function appendStaticMask(staticMask: number, index: number) { - return (staticMask | (1 << index)) >>> 0 + return (staticMask | (1 << (index - 1))) >>> 0 }📝 Committable suggestion
🤖 Prompt for AI Agents