T
TanStackβ€’16mo ago
afraid-scarlet

Is it possible to filter the route param with Tanstack Router?

I have several types of interfaces: - for /@{user} I need to show user profile page - for other (/, /game, /trends, /movie, ...) paths as /{section} I need to show section info page How can I do it?
27 Replies
rival-black
rival-blackβ€’16mo ago
what does "filter" mean? please explain in more detail
afraid-scarlet
afraid-scarletOPβ€’16mo ago
May be it is not correct word. I mean validate param value for the pattern match. paths as /@aaa, /@bbb, /@ccc and similar must show one page
rival-black
rival-blackβ€’16mo ago
a prefix? we don't have that yet
afraid-scarlet
afraid-scarletOPβ€’16mo ago
yes!
rival-black
rival-blackβ€’16mo ago
was requested a few times
afraid-scarlet
afraid-scarletOPβ€’16mo ago
😦 Бan it be done by some kind of plugin or middleware maybe? Or check the parameter using a regular expression and if it fits, then show the component, and if not, then look for a new route further?
rival-black
rival-blackβ€’16mo ago
you could read this https://discord.com/channels/719702312431386674/1272111729366798336/1272113119141036045 @enheit wanted to implement that but we were stuck when it came to suffixes so I guess we could drop suffixes and only do prefixes
afraid-scarlet
afraid-scarletOPβ€’16mo ago
prefix is enough for me πŸ™‚ is it not known when it will be implemented?
rival-black
rival-blackβ€’16mo ago
nope maybe @enheit has a plan for this
sensitive-blue
sensitive-blueβ€’16mo ago
Yes, I wanted to implement that, but I got stuck deciding how to support suffixes and prefixes without introducing breaking changes since there were no strict rules on route path dynamic segments. I can't confidently say that I'll implement this in the near future because I've decided not to spend too much time on it and to continue using what we have at the moment. If I find the time and desire to contribute in the near future, I'll open a PR. But if anyone is willing to work on it right now, feel free to contribute.
rival-black
rival-blackβ€’16mo ago
maybe dropping suffixes is the right choice here
afraid-scarlet
afraid-scarletOPβ€’16mo ago
or add matcher function to params option πŸ˜‰
rival-black
rival-blackβ€’16mo ago
if you are interested in implementing any of this, let me know so we could discuss details
afraid-scarlet
afraid-scarletOPβ€’16mo ago
I think to do something like this
GitHub
Comparing TanStack:main...KleinMaximus:main Β· TanStack/router
πŸ€– Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - Comparing TanStack:main...KleinMaximus:ma...
rival-black
rival-blackβ€’16mo ago
but this would still give you "@foo" as the path param and not remove the prefix
afraid-scarlet
afraid-scarletOPβ€’16mo ago
this can be used in conjunction with parse and stringify
rival-black
rival-blackβ€’16mo ago
hm not in a typesafe way I think
afraid-scarlet
afraid-scarletOPβ€’16mo ago
why?
rival-black
rival-blackβ€’16mo ago
just a gut feeling πŸ˜„ but happy to be proven wrong
afraid-scarlet
afraid-scarletOPβ€’15mo ago
we can use this feature not only for suffix or prefix it can be simple filter πŸ™‚ for example in one case we need number params and string in another for different routes and different target components of course it can be done in component directly not a completely correct example 😦 maybe the prefix can be implemented using masking?
afraid-scarlet
afraid-scarletOPβ€’15mo ago
Route Masking | TanStack Router React Docs
Route masking is a way to mask the actual URL of a route that gets persisted to the browser's history and URL bar. This is useful for scenarios where you want to show a different URL than the one that is actually being navigated to and then falling back to the displayed URL when it is shared and (optionally) when the page is reloaded. Here's a ...
compatible-crimson
compatible-crimsonβ€’14mo ago
Or maybe this could be only supported via virtual routes for those that need more complex matchers? πŸ€” In my case, at least at the moment, I'd "only" need the @ prefix like @[username] or @$username, maybe via custom regex πŸ€·β€β™€οΈ tho not sure about types 😬
rival-black
rival-blackβ€’14mo ago
what does this have to do with virtual routes?
compatible-crimson
compatible-crimsonβ€’14mo ago
My thought was that maybe this more complex matching, be it a prefix or suffix, could be somehow supported via defineVirtualSubtreeConfig instead of trying to support it in file paths, maybe that could make it somewhat simpler, tho I might misunderstand something here πŸ˜…
rival-black
rival-blackβ€’14mo ago
hm no, virtual routes (currently) only are a way to map your arbitrarily named files to routes but the route specification itself does not change
compatible-crimson
compatible-crimsonβ€’14mo ago
uh I see so it would need to be supported in file routes as well or virtual routes would have to be extended somehow I guess
rival-black
rival-blackβ€’14mo ago
yes

Did you find this page helpful?