T
TanStack4mo ago
deep-jade

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
adverse-sapphire
adverse-sapphire4mo ago
extract the common parts in components and reuse the components in the routes ?
deep-jade
deep-jadeOP4mo ago
Hmm. There’s just a bunch of layout code in the routes that would be duplicated. I’d like to avoid that.
adverse-sapphire
adverse-sapphire4mo ago
move the layout components outside as well then?
deep-jade
deep-jadeOP4mo ago
It just feels stupid to have the same exact sub-routes twice. Especially when using file-based routing.
adverse-sapphire
adverse-sapphire4mo ago
how come they are exactly the same? some have a path param, some dont?
deep-jade
deep-jadeOP4mo 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
adverse-sapphire
adverse-sapphire4mo 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
deep-jade
deep-jadeOP4mo 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.
adverse-sapphire
adverse-sapphire4mo ago
how would code based routing help here?
deep-jade
deep-jadeOP4mo ago
Hmm. Wait no. You’re right. That makes no sense.

Did you find this page helpful?