T
TanStack7mo ago
metropolitan-bronze

Avoiding duplicate routing

Take the following routing example: posts/$postId posts/$postId/meta posts/$postId/settings posts/$postId/content posts/new posts/new/meta posts/new/settings posts/new/content How would I avoid having practically duplicated routes while keeping the ability to differ from the layout in some cases for either new/existing entities?
10 Replies
vicious-gold
vicious-gold7mo ago
extract the common parts in components and reuse the components in the routes ?
metropolitan-bronze
metropolitan-bronzeOP7mo ago
Hmm. There’s just a bunch of layout code in the routes that would be duplicated. I’d like to avoid that.
vicious-gold
vicious-gold7mo ago
move the layout components outside as well then?
metropolitan-bronze
metropolitan-bronzeOP7mo ago
It just feels stupid to have the same exact sub-routes twice. Especially when using file-based routing.
vicious-gold
vicious-gold7mo ago
how come they are exactly the same? some have a path param, some dont?
metropolitan-bronze
metropolitan-bronzeOP7mo ago
Yep. Basically. They just have to fetch the entity differently, one is stored in localstorage and doesn’t have an id yet, the other one is fetched from API. Also. Not all are the same, the one without a path parameter has some sub routes that are different. But it does have the same sub routes as the one with a path parameter. The difference between them is basically just that one has an existing entity in backend, the other doesn’t yet
vicious-gold
vicious-gold7mo ago
if you could wish for something, how would you want to handle it? right now I have a somewhat hacky solution which would be: remove the new routes, and treat postId=new as the special case in your code
metropolitan-bronze
metropolitan-bronzeOP7mo ago
Hmm. Maybe code-based routing could solve it? So if you could mix file-based routing and code-based routing. But yeah, that was the solution Copilot and I came up with too.
vicious-gold
vicious-gold7mo ago
how would code based routing help here?
metropolitan-bronze
metropolitan-bronzeOP7mo ago
Hmm. Wait no. You’re right. That makes no sense.

Did you find this page helpful?