T
TanStack4d ago
unwilling-turquoise

Route Context Persistency

Is the route context persistent in the server like a session? If I save the logged user there, is it available only to the client that originated it's value assignment? That was the case for router alone, but now that loaders are executed in the server, I am not sure how it works.
13 Replies
like-gold
like-gold4d ago
route context only lives as long as the request does in fact the each router instance on the server only lives as long as the request
unwilling-turquoise
unwilling-turquoiseOP4d ago
Got it. Is the user navigating to another route in the same tab a new request? I am curios of how the queryClient keeps it's cache on the server. I am experimenting with a query that takes the logged user in the root before loader with ensureQueryData. I noticed that only the first navigation executes the queryFn, If I click on a Link, the queryFn doesn't run, but the value is still in the cache are routes are ssr: data-only
like-gold
like-gold4d ago
Is the user navigating to another route in the same tab a new request?
no. thats a client side navigation only the initial SSR request is a server side request (when it comes to router)
I am curios of how the queryClient keeps it's cache on the server.
there is no query cache on the server at least not across requests
like-gold
like-gold4d ago
TanStack Query Integration | TanStack Router Docs
[!IMPORTANT] This integration automates SSR dehydration/hydration and streaming between TanStack Router and TanStack Query. If you haven't read the standard guide, start there. What you get Automatic...
unwilling-turquoise
unwilling-turquoiseOP4d ago
Yes
like-gold
like-gold4d ago
the queries run on the server initally, then the query cache is streamed to the client and it picks it up from there
unwilling-turquoise
unwilling-turquoiseOP4d ago
It's working great. But for this particular use case, I just need to fetch the user using a cookie from the request and store the user in the context So each protected route has a beforeLoad to check if the user is present in the context, if not redirect to login Will that work? Based on what you said, I don't need to use queryClient for that, just call the serverFn that fetches the user directly. My doubt is if the context will be available in the beforeLoaders of the other routes when doing clientSide navigations And also I don't want to make that user fetch on every nav, only on the initial one. Will that work or should I setup start session like the doc says?
like-gold
like-gold4d ago
just put the user in query cache then you can easily call it for each navigation
unwilling-turquoise
unwilling-turquoiseOP4d ago
but like that I would need to call it on top of every page component, right? It will not be available in beforeLoaders (server)
like-gold
like-gold4d ago
maybe provide a complete example on what you want to do I miss some context
unwilling-turquoise
unwilling-turquoiseOP4d ago
No description
No description
No description
unwilling-turquoise
unwilling-turquoiseOP4d ago
We are using that approach with router alone, I am looking for the easiest way to migrate to start. It worked before because the loaders/beforeLoaders/context were clientSide. To summarize. I need to fetch the current user in initial navigation and cache it. Check for the user in beforeLoaders without having to fetch for the user again and guard the route. New user fetch should only happens on initial request (refresh, open new tab, etc.) In advance, thanks a lot for your time

Did you find this page helpful?