T
TanStack4d ago
absent-sapphire

Multiple optional parameters

Hey everyone! Is it possible to create 2 optional parameters in root with tanstack router? We are rewriting the project and need to preserve the current URL structure. In documentation I found example for i18n https://tanstack.com/router/latest/docs/framework/react/guide/path-params#complex-i18n-patterns but in my case routes should have both optional language and city. For example ${-$locale}/${-$city}/event/${event-slug} This should support URLs like:
/it/rome/event/andrea-bocelli // both locale and city
/fr/paris/event/andrea-bocelli // both locale and city

/it/event/andrea-bocelli // only locale
/fr/event/andrea-bocelli // only locale

/rome/event/andrea-bocelli // only city
/paris/event/andrea-bocelli // only city

/event/andrea-bocelli // not selected locale and city
/it/rome/event/andrea-bocelli // both locale and city
/fr/paris/event/andrea-bocelli // both locale and city

/it/event/andrea-bocelli // only locale
/fr/event/andrea-bocelli // only locale

/rome/event/andrea-bocelli // only city
/paris/event/andrea-bocelli // only city

/event/andrea-bocelli // not selected locale and city
Static page about working correctly with both optional parameters /it/rome/about /it/about and /about Event page also working if both parameters exist or none of them /it/rome/event/andrea-bocelli or /event/andrea-bocelli However with 1 parameter I got not found error /it/event/andrea-bocelli or. /rome/event/andrea-bocelli Stackblitz with reproduce https://stackblitz.com/edit/tanstack-router-lvn6j2tq?file=src%2Froutes%2F%7B-%24locale%7D%2F%7B-%24city%7D%2Fabout.tsx
7 Replies
vicious-gold
vicious-gold4d ago
Path Params | TanStack Router React Docs
Path params are used to match a single segment (the text until the next /) and provide its value back to you as a named variable. They are defined by using the $ character prefix in the path, followed...
vicious-gold
vicious-gold4d ago
seems like it should be possible unless there is a bug or it's an ambiguity issue well, yes in fact, it must be that if you have only one segment before /event, how would the router know if it should take the place of locale or city /foo/event/my-event foo: is it a locale or a city ? maybe you need a segment between the two params /{-$locale}/app/{-$city}/event/{-$slug}
absent-sapphire
absent-sapphireOP4d ago
Yeah but I can't do that because that structure exist on current project. Google and other search engines indexed with that URL structure Interseting that about page working correctly - /it/rome/about - /it/about
- /about All of them match about component
vicious-gold
vicious-gold4d ago
yeah, that's true maybe in the case of ambiguity it matches the last segment but maybe there's a bug when there is a third optional param
absent-sapphire
absent-sapphireOP4d ago
Maybe cause it's working with duplicated directory on first level {-$locale}/event/{-$slug} Strange that you need to duplicate dynamic segment but not static one
absent-sapphire
absent-sapphireOP4d ago
Found open pull request with fix for my case. Probably should be working with multiple optional params after merge https://github.com/TanStack/router/pull/5176
GitHub
fix(router-core): optional-path-param usage variation matches by nl...
#4933 highlighted a case where index routes would not resolve when an optional param is followed by a required path param. this pr resolves #4933 apart from this specific issue, this PR updates isM...
protestant-coral
protestant-coral4d ago
cc @Nico Lynzaad can you please add a test case for the above to your PR? also, what's holding this PR back? let me knof if you need anything

Did you find this page helpful?