T
TanStack10mo ago
quickest-silver

Run Loader on Client

Is it possible to force the loader to run on the client? I'd like to call tRPC, but our procedure return user-specific results.
If the loader is called from the server I close context about the active user.
13 Replies
quickest-silver
quickest-silver10mo ago
how does the client know about the active user?
quickest-silver
quickest-silverOP10mo ago
Technically it doesn't, the user id is injected as a HTTP Header by a proxy
quickest-silver
quickest-silver10mo ago
and where is that proxy running? I have trouble understanding your setup
quickest-silver
quickest-silverOP10mo ago
In between the Tanstack server and client's browser. (The proxy handles authentication and injects the header for any future requests) So when we were on tanstack router, the loader always ran on the client. We could call trpc which would make a request from the client, have the header injected and trpc could return the results. On tanstack start, the loader request happens on the server. Since it doesn't originate from the client, no header is injected and tRPC thinks the request is unauthenticated.
quickest-silver
quickest-silver10mo ago
so can't render this route on the server you disable it via route options ssr:false
quickest-silver
quickest-silverOP10mo ago
That works, but only if the client is already active. If they try to visit that route as their first pageview, the loader is still called on the server
quickest-silver
quickest-silver10mo ago
really? if it is not server rendered, is the loader still called on the server? probably correct so you could use createIsomorphicFn to define a function with different client/server behavior but would that really work in this case? you would need to retrigger the loader when route is rendered
quickest-silver
quickest-silverOP10mo ago
Yeah, I'm not sure that would work here. The loader isn't being called on the client at all on that first load. Even if we could re-trigger the loader, I think using createIsomorphicFn would add a little too much confusion here for developers just reading the code. What's the idomatic approach for this in tanstack start? How should you validate authentication in a server loader? The examples I believe are focus more on server functions and API routes.
quickest-silver
quickest-silver10mo ago
typically you know something in the server function .e.g a cookie, a header, anything you would just create a server function and call that from the loader so the server that handles SSR, is that not called via the proxy when the browser navigates to it?
quickest-silver
quickest-silverOP10mo ago
It is, tanstack is serving everything including trpc. So you're saying that the loader retains the context of the original request?
quickest-silver
quickest-silver10mo ago
a server function does and if the loader calls a server function, you can access the request
quickest-silver
quickest-silverOP9mo ago
Makes sense. I’m not sure I’m ready to move these over to server functions yet until they’re a little more mature. But I’ll keep it on the shelf and re-evaluate. Thanks for your help @Manuel Schiller
quickest-silver
quickest-silver9mo ago
then your best bet is isomorphic functions. there the server impl will also be able to access the request

Did you find this page helpful?