T
TanStack•2mo ago
xenial-black

Can I check if a route exactly matches the current location inside a loader function ?

Hey, I feel I might be going against the intended flow of TanStack Router here, but here I go anyway: My route structure goes like this: /app/channel/${channelId}/show/${showId}/some_page We store the current channel and show IDs in the URL and those follow the user anywhere he might go in the app. After logging in, the user is directed to /app/channel, the intent is to find the best channelId through some business logic. When we found the appropriate channelId, we throw redirect({ to: /app/channel/${channelId}/show}); where, again, we try to find the most appropriate showId. When we do, the user is FINALLY redirected to the home page. Problem is, if the user, has bookmarked a "complete URL" with a channel and show IDs, then I don't want to execute my business logic trying to fill those voids. Therefore, I'm trying to determine in the loader function of my createFileRoute if the location exactly matches the current route (say, /app/channel) which is a sign the user has not provided IDs and we must fill it for them. (Indeed, I found that accessing a nested route triggers the loader function of the parent routes). But I can't seem to find the best way to achieve that. Again, my initial approach might be utterly flawed. Thanks in advance for your help!
3 Replies
metropolitan-bronze
metropolitan-bronze•2mo ago
use .index on /app/channel and app/channel/$channelId/show .index only matches when exactly on that route, .route (which is the default) tries to nest
xenial-black
xenial-blackOP•2mo ago
Thank you so much, I was confused with the difference between index and route turns out this is exactly what I needed! 🙂
metropolitan-bronze
metropolitan-bronze•2mo ago
I still feel like .index should be the default behavior and .route should be renamed to .layout. Like, I got used to it, but it still feels odd and I see countless other people who try to adopt tanstack router that fall for it. (I also fell for it when I started) Otherwise, tanstack is a beast router

Did you find this page helpful?