Skip to content

fix(router): support suffix wildcards in TrieRouter - #5230

Closed
Kjubikstronk wants to merge 1 commit into
honojs:mainfrom
Kjubikstronk:fix-trie-router-suffix-wildcard
Closed

Kjubikstronk wants to merge 1 commit into
honojs:mainfrom
Kjubikstronk:fix-trie-router-suffix-wildcard

Conversation

@Kjubikstronk

Copy link
Copy Markdown

Closes #5219.

What was happening

The sub-app in the report turns out to be a red herring. TrieRouter never supported suffix wildcards at all:

const app = new Hono({ router: new TrieRouter() })
app.get('/assets*', (c) => c.text('ok'))
await app.request('http://x/assets/app.js') // 404, no sub-app anywhere

getPattern() only treats * as a wildcard when the label is exactly *, so /assets* is stored as a literal child keyed "assets*", which no request path can equal.

It's the only router with that gap:

Router GET /assets* → /assets/app.js
RegExpRouter 200
PatternRouter 200
LinearRouter 200
TrieRouter 404

That explains the sub-app: sub.post('/items') alongside sub.post('/:slug') makes RegExpRouter throw UnsupportedPathError, so SmartRouter falls back to TrieRouter and the pre-existing gap surfaces. It also explains the three narrowing results in the issue — dropping either route, using different methods, or writing {slug} all keep RegExpRouter viable, and the wildcard keeps working. /assets/* is unaffected because that path ends in a bare *, which was already handled.

Worth noting the failure mode: an app silently changes routing behaviour based on which router SmartRouter settles on, and adding an unrelated route elsewhere can flip it.

The change

The prefix is registered on the parent node when the route is inserted, and matched against the rest of the path at search time — a trailing * consumes everything after the prefix, which is what the other routers do. No params are captured, matching them.

The offset computation the regex branch already used is pulled into a small helper and shared, since both need the rest of the path rather than a single part. It stays lazy, so paths without either construct don't pay for it.

The new field is optional and only allocated for routes that actually use a suffix wildcard.

Verification

TrieRouter now agrees with RegExpRouter on every case I probed — 2 routes × 9 paths, including /assets (empty remainder), /assetsfoo (partial segment) and /assets/a/b (crossing /):

RegExp  /a/b*     /a/b=1 /a/bc=1 /a/b/c=1 /a/c=0 /a/=0 /a/zzz=0 /assets=0 /assetsfoo=0 /xy=0
Trie    /a/b*     /a/b=1 /a/bc=1 /a/b/c=1 /a/c=0 /a/=0 /a/zzz=0 /assets=0 /assetsfoo=0 /xy=0
RegExp  /assets*  /a/b=0 /a/bc=0 /a/b/c=0 /a/c=0 /a/=0 /a/zzz=0 /assets=1 /assetsfoo=1 /xy=0
Trie    /assets*  /a/b=0 /a/bc=0 /a/b/c=0 /a/c=0 /a/=0 /a/zzz=0 /assets=1 /assetsfoo=1 /xy=0

Before the change, a cross-router comparison over that matrix showed 8 disagreements; afterwards 1, and that one is byte-identical before and after (see below). The reproduction from the issue passes, still resolving to SmartRouter + TrieRouter, and the sub-app's own routes still respond.

pnpm vitest --project main: 122 files, 3800 passed, 30 skipped, 0 failures — against 3784/28 on main. tsc -p tsconfig.spec.json, ESLint and Prettier are clean.

Tests

Added to the shared router cases in common.case.test.ts, so every router is held to the same behaviour rather than only TrieRouter.

Two things that came up, both pre-existing and left alone:

  • Below the root, LinearRouter and PatternRouter ignore the wildcard's prefix — /a/b* also matches /a/c, /a/, /a/zzz. PatternRouter goes further and matches /assets too. Rather than weaken the new case to accommodate that, I've added skip entries with the reason, which keeps the divergence visible. Happy to open a separate issue.
  • LinearRouter matches /assetsfoo against /assets/*, where RegExpRouter and TrieRouter do not. Untouched by this change and identical before and after.

I have not touched non-trailing wildcards such as /x*/y. RegExpRouter supports those and TrieRouter still does not; it seemed better kept separate from the reported bug.

TrieRouter stored `/assets*` as a literal segment named `assets*`, so it
never matched anything. RegExpRouter, PatternRouter and LinearRouter all
support the pattern, so whether such a route worked depended on which
router SmartRouter happened to settle on.

The prefix is now registered on the parent node at insert time and
matched against the rest of the path, which is what a trailing `*` means
in the other routers. No params are captured, matching them as well.

The shared router cases cover it. LinearRouter and PatternRouter skip the
nested case, which they get wrong independently of this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suffix wildcard routes (/foo*) return 404 when a sub-app has static + :param routes with the same method

2 participants