Skip to content

findRoute returns incorrect route when multiple matches exist (from v0.7.0) #166

@rinckodev

Description

@rinckodev

Environment

Environment
rou3 version: 0.7.0 and above (bug present), works correctly on 0.6.3

Tested in:
Node.js version: 22.14.0
Bun version: 1.2.18

Reproduction

Minimal reproduction:

import { createRouter, addRoute, findRoute } from "rou3";

const router = createRouter();

[
  "settings/:tab",
  "supporters/:menu",
].map(route => addRoute(router, "button", route, { id: route }));

console.log(findRoute(router, "button", "supporters/icons"));

Output (v0.7.0 and up):

{
  data: {
    id: "settings/:tab",
  },
  params: {
    tab: "icons",
  },
} // ❌ incorrect

If I reverse the registration order like this:

[
  "supporters/:menu",
  "settings/:tab",
]

Then the output is:

{
  data: {
    id: "supporters/:menu",
  },
  params: {
    menu: "icons",
  },
} // ✅ correct

Expected behavior:
The router should match the route with the most specific static prefix (supporters/:menu) regardless of the order it was registered.

This behavior works as expected in version 0.6.3.

Image

Describe the bug

Starting from version 0.7.0, the findRoute function began returning less specific routes in situations where multiple dynamic routes could match the same path. For example, when registering the routes "settings/:tab" and "supporters/:menu", a lookup for "supporters/icons" incorrectly returns "settings/:tab", even though the path clearly starts with "supporters" and there is a matching route with that prefix. The behavior seems to vary depending on the registration order, suggesting that route matching may no longer be prioritizing more specific or more static prefixes correctly. This issue does not occur in version 0.6.3.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions