Skip to content
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

fix: 404 Cannot find any route matching, when adding a child route. #103

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
28 changes: 23 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,23 @@
if (node && node.placeholderChildren.length > 1) {
// https://github.com/unjs/radix3/issues/95
const remaining = sections.length - i;
// prioritize items with the exact maxDepth as remaining
const sortedPlaceholderChildren = [...node.placeholderChildren].sort(
(a, b) => {
if (a.maxDepth === remaining && b.maxDepth === remaining) {
return 0;

Check warning on line 73 in src/router.ts

View check run for this annotation

Codecov / codecov/patch

src/router.ts#L73

Added line #L73 was not covered by tests
} else if (a.maxDepth === remaining) {
return -1;
} else if (b.maxDepth === remaining) {
return 1;
} else {
return b.maxDepth - a.maxDepth;
}
},
);

node =
node.placeholderChildren.find((c) => c.maxDepth === remaining) ||
sortedPlaceholderChildren.find((c) => c.maxDepth >= remaining) ||
null;
} else {
node = node.placeholderChildren[0] || null;
Expand Down Expand Up @@ -138,14 +153,17 @@
childNode.paramName = section.slice(3 /* "**:" */) || "_";
isStaticRoute = false;
}

matchedNodes.push(childNode);
node = childNode;
}

matchedNodes.push(childNode);
}

for (const [depth, node] of matchedNodes.entries()) {
node.maxDepth = Math.max(matchedNodes.length - depth, node.maxDepth || 0);
for (const [depth, matchedNode] of matchedNodes.entries()) {
matchedNode.maxDepth = Math.max(
matchedNodes.length - depth,
matchedNode.maxDepth || 0,
);
}

// Store whatever data was provided into the node
Expand Down
31 changes: 31 additions & 0 deletions tests/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@ describe("Router lookup", function () {
"route/with/trailing/slash": { path: "route/with/trailing/slash/" },
});
});

describe("routes with lower maxDepth should be considered too", function () {
testRouter(
[
"/",
"/:packageAndRefOrSha",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one triggers the if (node && node.placeholderChildren.length > 1) { condition.

"/:owner/:repo/",
"/:owner/:repo/:packageAndRefOrSha",
"/:owner/:repo/:npmOrg/:packageAndRefOrSha",
],
{
"/tinylibs/tinybench/tiny@232": {
path: "/:owner/:repo/:packageAndRefOrSha",
params: {
owner: "tinylibs",
repo: "tinybench",
packageAndRefOrSha: "tiny@232",
},
},
"/tinylibs/tinybench/@tinylibs/tiny@232": {
path: "/:owner/:repo/:npmOrg/:packageAndRefOrSha",
params: {
owner: "tinylibs",
repo: "tinybench",
npmOrg: "@tinylibs",
packageAndRefOrSha: "tiny@232",
},
},
},
);
});
});

describe("Router insert", function () {
Expand Down