Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions packages/router-core/src/new-process-route-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,22 +895,8 @@ function getNodeMatch<T extends RouteLike>(

const isBeyondPath = index === partsLength
if (isBeyondPath) {
if (node.route && (!pathIsIndex || node.kind === SEGMENT_TYPE_INDEX)) {
if (isFrameMoreSpecific(bestMatch, frame)) {
bestMatch = frame
}

// perfect match, no need to continue
// this is an optimization, algorithm should work correctly without this block
if (
statics === partsLength &&
!dynamics &&
!optionals &&
!skipped &&
node.kind === SEGMENT_TYPE_INDEX
) {
return bestMatch
}
if (node.route && !pathIsIndex && isFrameMoreSpecific(bestMatch, frame)) {
bestMatch = frame
}
// beyond the length of the path parts, only index segments, or skipped optional segments, or wildcard segments can match
if (!node.optional && !node.wildcard && !node.index) continue
Expand All @@ -919,6 +905,28 @@ function getNodeMatch<T extends RouteLike>(
const part = isBeyondPath ? undefined : parts[index]!
let lowerPart: string

// 0. Try index match
if (isBeyondPath && node.index) {
const indexFrame = {
node: node.index,
index,
skipped,
depth: depth + 1,
statics,
dynamics,
optionals,
}
// perfect match, no need to continue
// this is an optimization, algorithm should work correctly without this block
if (statics === partsLength && !dynamics && !optionals && !skipped) {
return indexFrame
}
if (isFrameMoreSpecific(bestMatch, indexFrame)) {
// index matches skip the stack because they cannot have children
bestMatch = indexFrame
}
}

// 5. Try wildcard match
if (node.wildcard && isFrameMoreSpecific(wildcardMatch, frame)) {
for (const segment of node.wildcard) {
Expand All @@ -937,9 +945,10 @@ function getNodeMatch<T extends RouteLike>(
if (casePart !== suffix) continue
}
// the first wildcard match is the highest priority one
// wildcard matches skip the stack because they cannot have children
wildcardMatch = {
node: segment,
index,
index: partsLength,
skipped,
depth,
statics,
Expand Down Expand Up @@ -1048,19 +1057,6 @@ function getNodeMatch<T extends RouteLike>(
})
}
}

// 0. Try index match
if (isBeyondPath && node.index) {
stack.push({
node: node.index,
index,
skipped,
depth: depth + 1,
statics,
dynamics,
optionals,
})
}
}

if (bestMatch && wildcardMatch) {
Expand Down
Loading