How do you execute a function/middleware only in a server-side context for a given route?
I am trying to implement Privy's server-side middeware setup, which has a NextJS middleware example of how to set up a middleware with a matcher for certain routes. I want to replicate this functionality either in a server function or server middleware, but I am not quite sure how to do this. Since
beforeLoad
and loader
can execute on the server or the client, I only want the functionality in this example to execute on the server. What is the best way to approach this?Privy Docs
Configure cookies - Privy docs
4 Replies
adverse-sapphireOP•5mo ago
I already somewhat have this functionality using
createIsomorphicFn
but not sure if that is the proper use case for thisstormy-gold•5mo ago
i use a pathless route and beforeLoad for this
grabbing auth context via api call and inject into router
metropolitan-bronze•5mo ago
it can work, it depends WHERE you put that function then
if there is a way to navigate there on the client without triggering the function on the serverside first, then it wont work
so it must be placed e.g. in the root route
otherwise, running this inside of a server function ensures it always executes on the server
adverse-sapphireOP•5mo ago
I ended up using a
createIsomorphicFn
to handle detection and then I have client-side code for handling the token refresh. Thanks!