T
TanStack7mo ago
national-gold

Can I put all my redirects in a single file?

I'm rewriting an existing web app, and I have a bunch of routes that I'm looking to redirect. (I want old routes that a user might have as a bookmark to still take them to the appropriate place) I'm wondering if there's a way for me to handle all the redirects I need to set up in the application in one spot? Example: I have multiple old pages that will be getting combined into one: /prospects/contacts and /prospects/overview. Both of these routes will end up being served on a single page /contacts in the new app. From what I understand with tanstack router– I could set up a directory prospects and have two files contacts.tsx and overview.tsx that each have a fileRoute with beforeLoad and throw redirect to the /contacts route. Since I have about 10 other places that follow this example pattern– I'm hoping that I can handle it all in one place rather than needing a bunch of directories/files that solely exist for redirecting old routes. Does this question make sense? Thanks!
3 Replies
flat-fuchsia
flat-fuchsia7mo ago
if you don't define those routes you would run into router not finding a matching route we will be adding a rewrite hook in future that will allow that you might be able to hack router to add routes via code, I need to try this later and will post an example if it works
national-gold
national-goldOP7mo ago
thanks Manuel, I'll go ahead and just roll with a ~dozen placeholder rewrite files in the project for now with the hope that I can combine/simplify it in the future. Appreciate the quick response!
flat-fuchsia
flat-fuchsia7mo ago
the nice thing about that is that you also get search params typesafety (if you used them)

Did you find this page helpful?