Server-Side Helpers
The server-side helpers provides you with a set of helper functions that you can use to prefetch queries on the server. This is useful for SSG, but also for SSR if you opt not to use ssr: true.
Prefetching via the server-side helpers allows populating the query cache on the server, which means that these queries do not have to fetch on the client initially.
There are 2 ways to use the server-side helpers.
1. Internal router
This method is used when you have direct access to your tRPC router. e.g. when developing a monolithic Next.js application.
Using the helpers makes tRPC call your procedures directly on the server, without an HTTP request, similar to server-side calls.
That also means that you don't have the request and response at hand like you usually do. Make sure you're instantiating the server-side helpers with a context without req & res, which are typically filled via the context creation. We recommend the concept of "inner" and "outer" context in that scenario.
ts
ts
2. External router
This method is used when you don't have direct access to your tRPC router. e.g. when developing a Next.js application and a standalone API hosted separately.
ts
ts
Helpers usage
The server-side helpers methods return an object much like the tRPC client, with all of your routers as keys. However, rather than useQuery and useMutation, you get prefetch, fetch, prefetchInfinite, and fetchInfinite functions.
The primary difference between prefetch and fetch is that fetch acts much like a normal function call, returning the result of the query, whereas prefetch does not return the result and never throws - if you need that behavior, use fetch instead. Instead, prefetch will add the query to the cache, which you then dehydrate and send to the client.
ts
ts
The rule of thumb is prefetch for queries that you know you'll need on the client, and fetch for queries that you want to use the result of on the server.
The functions are all wrappers around react-query functions. Please check out their docs to learn more about them in detail.
For a full example, see our E2E SSG test example
Next.js Example
pages/posts/[id].tsxtsx
pages/posts/[id].tsxtsx