solidjs / solidjs/solid-router
[Feature] Allow optional permutations when exclusive matchFilters are present
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 1.3k
- Forks
- 180
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 19
Description
Describe the bug
I was having issues with a routing setup where I have multiple optionals in a row in a path, but each has exclusive matchFilters to differentiate them. I looked through the code and found:
export function expandOptionals(pattern: string): string[] {
let match = /(\/?\:[^\/]+)\?/.exec(pattern);
if (!match) return [pattern];
let prefix = pattern.slice(0, match.index);
let suffix = pattern.slice(match.index + match[0].length);
const prefixes: string[] = [prefix, (prefix += match[1])];
// This section handles adjacent optional params. We don't actually want all permuations since
// that will lead to equivalent routes which have the same number of params. For example
// `/:a?/:b?/:c`? only has the unique expansion: `/`, `/:a`, `/:a/:b`, `/:a/:b/:c` and we can
// discard `/:b`, `/:c`, `/:b/:c` by building them up in order and not recursing. This also helps
// ensure predictability where earlier params have precidence.
while ((match = /^(\/\:[^\/]+)\?/.exec(suffix))) {
prefixes.push((prefix += match[1]));
suffix = suffix.slice(match[0].length);
}
return expandOptionals(suffix).reduce<string[]>(
(results, expansion) => [...results, ...prefixes.map(p => p + expansion)],
[]
);
}
And while I understand the sentiment in the comment, I believe this design should be rethought for routes where there are exclusive matchFilters for each of the optionals. For example, the case where you have:
const matchFilters = {
a: ["hello","world"],
b: ["foo","bar"],
c: /^\d+$/
}
// ...
<Route path="/:a?/:b?/:c?" matchFilters={matchFilters} /* ... */ />
// ...
There is no reason why all permutations of this route shouldn't be available, as the matchFilters for all three routes are mutually exclusive. A workaround for this is to use an array of paths with only one optional per path. When used with matchFilters this technique allows the functionality, but requires manually permuting the list of optionals to create the array -- for example the above becomes:
<Route path={["/:a/:b/:c?", "/:a/:c?", "/:b/:c?", "/:c?"]} matchFilters={matchFilters} />
which works as described.
Your Example Website or App
n/a
Steps to Reproduce the Bug or Issue
nothing to reproduce -- current functionality is working as intended, would just be nice if matchFilters were taken into account when generating optional routes
Expected behavior
When matchFilters are present, allow permutations of optional parameters as long as the matchFilters for all parameters are mutually exclusive
Screenshots or Videos
No response
Platform
n/a
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the expandOptionals function shown in the issue and trace how its generated paths are combined with matchFilters. Compare the current ordered expansions with the explicitly permuted path array in the example. Done means mutually exclusive matchFilters enable the equivalent optional permutations without requiring callers to list every path manually.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100