Prim+RPC supports Next.js through a route defined on its App Router. Next.js allows both Pages Router and App Router
folders to exist in the same project, so you may still use Prim+RPC if you have not yet made the transition to App
Router.
Let’s say that you have a module like so (you can place functions wherever you’d like). In this example, functions are
defined inside of the app router and we prefix the our function with an underscore to mark it as a private folder
co-located with our code in the app router:
app
prim
[[...prim]]
_functions
index.ts
You may configure Prim+RPC with Next.js by using a catch-all route in your app directory:
app
prim
[[...prim]]
route.ts
Your Prim+RPC server is now set up! Next.js is a fullstack framework that can run on both server and client, which means
the Prim+RPC client may be called on the server as well as the client.
On the server, you can avoid a network request from the client by conditionally passing the module. Below is an example
of how you may do so, using the Fetch API method plugin:
app
prim
[[...prim]]
_client.ts
Now this exported client may used from anywhere in your project. Let’s demonstrate this first by setting up a client
component that will call the function from the client:
components
ClientRendered.tsx
And we can use this component in our page, alongside the client used on the server: