Hooks in Router Context/Preventing multiple server function requests in one page load
Hi, I've made an
AuthContext that uses a getUser server function to retrieve the authenticated user and pass it to the client so it can be used in a useAuth hook. Currently, to protect routes and redirect away if unauthenticated, I request this getUser server function again in the root route to add the user object to the route context, so I can check if it exists in beforeLoad and redirect if it does not. The problem with this setup is that it makes the request to the getUser server function twice in one page load, one to add it to the router context, and another in the AuthContext. I saw that with TanStack Router, one way to get around this would be to use the RouterProvider so I can use the AuthContext in the router context as well, but I see that RouterProvider is not available in TanStack Start. Am I overthinking/overcomplicating things or is there another way to add the user object to the router context without a second request to my server function?