How do I stop server code getting leaked to the client?
Hi, I have an util that should only be used by server code in orpc that leverages the
crypto
module, but based on my browser logs I am getting errors such as:
My router is currently set up this way:
and my client:
I saw online that you could use the serverOnly()
function to wrap the router to solve this but it provides an error:
Error: serverOnly() functions can only be called on the server!
5 Replies
fair-roseOP•3w ago
code minimal example here: https://github.com/kevmok/tanstackstart-server-orpc
https://codesandbox.io/p/github/kevmok/tanstackstart-server-orpc/main?import=true
cc: @Manuel Schiller
GitHub
GitHub - kevmok/tanstackstart-server-orpc
Contribute to kevmok/tanstackstart-server-orpc development by creating an account on GitHub.
deep-jade•3w ago
if that function is called on the client as well you should use createIsomorphicFn as well and don't supply a client impl
so it will be a noop on the client
deep-jade•3w ago
If you want to use this code ONLY in server side you should call these utils inside server functions or else, transform them in server functions.
deep-jade•3w ago
yep
fair-roseOP•3w ago
Got it, that makes sense
That could work, but moving all the orpc routes as server functions doesn't seem right in my head
Or should the orpc handler just call the server fn
Ok I found the root cause of everything. So the crypto utils were not really that big of a deal, but I have an orpc middleware in this
context
file and main issue is that it's importing db
and auth
(also imports db) so it was producing errors in my portalRoute
since it uses the protectedProcedure
... I would assume the createIsomorphicFn would handle this no? since the server
part is what's using the rpcRouter
and the client is just using an RPCLink
?
/src/server/api/utils/context.ts
i think just separating the imports in the orpc file worked ðŸ˜