Authenticating createLazyFileRoute
So I know that beforeLoad exists in createFileRoute but not createLazyFileRoute. How would I authenticate my lazy route in this case?
9 Replies
conscious-sapphireOP•2y ago
Currently trying out this idea, hopefully it works: https://github.com/TanStack/router/discussions/1265#discussioncomment-8851035
GitHub
Handle only protected routes except the login routes · TanStack rou...
Hello, I have followed the authenticated routes with context example now which already showed got me a working auth setup. But I'm not sure how I can add the beforeLoad authenticated check to a...
fascinating-indigo•2y ago
Yup, this all ties back to this concept here.
https://tanstack.com/router/latest/docs/framework/react/guide/code-splitting#how-does-tanstack-router-split-code
Code Splitting | TanStack Router Docs
Code splitting and lazy loading is a powerful technique for improving the bundle size and load performance of an application.
Reduces the amount of code that needs to be loaded on initial page load
fascinating-indigo•2y ago
Tanner's working on an experimental feature to make codesplitting even easier, so you'll just be able to use
createFileRoute and have codesplitting already handled for you.
But alas, its not ready for primetime just yet
(Its kinda neat though, it reduced my route files by half, since I no longer needed route.tsx and route.lazy.tsx)conscious-sapphireOP•2y ago
Thanks for responding @Sean Cassiere
I can confirm that this works
Also the kitchen-sink-file-based example is very helpful. Basically for anyone that has this issue, to authenticate lazy routes, you want to use _ layouts. eg.
[underscore].tsx and [underscore].index.tsx conscious-sapphireOP•2y ago
The only issue is that whenever I save _.index.lazy.tsx, it changes the route to "//"

conscious-sapphireOP•2y ago
Not sure if this is a bug or not allowed but it works!
solution: '//' as keyof FileRoutesByPath
fascinating-indigo•2y ago
You should name your layout something. Something like _authenticated would make it clear that any route under that layout requires authentication. That would probably fix the // issue as well.
conscious-sapphireOP•2y ago
yeah i probably should, _authenticated is too long lol. I guess I'll use directories. Thanks!
fascinating-indigo•2y ago
It could even just be
_a