T
TanStack13mo ago
exotic-emerald

Nested but shared routes

I love tanstack router combined with tanstack query - it’s solid AF. But despite all of that I’m still hitting a roadblock. I’ve got 2 pages with layout - _layout - _layout/A - _layout/B But now consider this: - _layout - _auth - _layout/_auth/A - _layout/B - _auth/C Is there a way to reuse _auth across the routes? Imagine that _auth in both cases do the same - it contains the same loader function.
7 Replies
vicious-gold
vicious-gold13mo ago
not sure what you mean by "reuse"? why not just nest _layout inside _auth?
exotic-emerald
exotic-emeraldOP13mo ago
I would like to reuse the _auth logic in two different routes: - _layoutA/_auth/A - _layoutB/_auth/B - _auth/C as far as I understand in this configuration each _auth should be defined as different “route” Btw thanks for the reply 🙇
vicious-gold
vicious-gold13mo ago
the other way round? _auth/_layout/A
exotic-emerald
exotic-emeraldOP13mo ago
I thought about it but then again what if I would need to reuse the same Layout?
vicious-gold
vicious-gold13mo ago
which layout? ideally supply a complete minimal example by forking one of the existing router examples on stackblitz
exotic-emerald
exotic-emeraldOP13mo ago
@Manuel Schiller https://stackblitz.com/edit/tanstack-router-yeiqnh Sorry that it took this long. Basically what I want to achieve is to reuse all of my pathless routes in different variations. Another example: I have one pathless route _auth declared in the routes root. Then I have _layout and _layoutDeprecated and both of those would also require _auth to work. But I still want to keep _auth in one separated file rather than putting _auth logic inside _layout and _layoutDeprecated. So i would like to have paths like this: _layout/_auth/XYZ and _layoutDeprecated/_auth/DSA Then in the future I could use _auth even in the different configuration like this: _auth/CXS So the same _auth would be used in non-nested page and in two nested pages.
Piotr Gacek
StackBlitz
Router Start Basic Auth Example (forked) - StackBlitz
Run official live example code for Router Start Basic Auth, created by Tanstack on StackBlitz
vicious-gold
vicious-gold13mo ago
with file based routing, you cannot "re-use" a route as you want it to do if you cannot nest under a single parent route, you could extract the common things of the layout route and reference them in multiple instances

Did you find this page helpful?