Repository navigation
fix(router): support suffix wildcards in TrieRouter - #5230
Closed
Kjubikstronk wants to merge 1 commit into
Closed
Kjubikstronk wants to merge 1 commit into
Kjubikstronk wants to merge 1 commit into
Conversation
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.
3 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #5219.
What was happening
The sub-app in the report turns out to be a red herring.
TrieRouternever supported suffix wildcards at all: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:
GET /assets*→/assets/app.jsThat explains the sub-app:
sub.post('/items')alongsidesub.post('/:slug')makesRegExpRouterthrowUnsupportedPathError, soSmartRouterfalls back toTrieRouterand 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 keepRegExpRouterviable, 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
SmartRoutersettles 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
TrieRouternow agrees withRegExpRouteron every case I probed — 2 routes × 9 paths, including/assets(empty remainder),/assetsfoo(partial segment) and/assets/a/b(crossing/):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 onmain.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 onlyTrieRouter.Two things that came up, both pre-existing and left alone:
LinearRouterandPatternRouterignore the wildcard's prefix —/a/b*also matches/a/c,/a/,/a/zzz.PatternRoutergoes further and matches/assetstoo. Rather than weaken the new case to accommodate that, I've addedskipentries with the reason, which keeps the divergence visible. Happy to open a separate issue.LinearRoutermatches/assetsfooagainst/assets/*, whereRegExpRouterandTrieRouterdo not. Untouched by this change and identical before and after.I have not touched non-trailing wildcards such as
/x*/y.RegExpRoutersupports those andTrieRouterstill does not; it seemed better kept separate from the reported bug.