#How do you execute a function/middleware only in a server-side context for a given route?

9 messages · Page 1 of 1 (latest)

formal onyx
#

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?

#

I already somewhat have this functionality using createIsomorphicFn but not sure if that is the proper use case for this

opal cedar
#

i use a pathless route and beforeLoad for this

#

grabbing auth context via api call and inject into router

formal onyx
#

How do you execute a function/middleware only in a server-side context for a given route?

latent cypress
#

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