Static Site Generation
Reference project: https://github.com/trpc/examples-next-prisma-todomvc
Static site generation requires executing tRPC queries inside getStaticProps on each page.
This can be done using server-side helpers to prefetch the queries, dehydrate them, and pass it to the page. The queries will then automatically pick up the trpcState and use it as an initial value.
Fetch data in getStaticProps
pages/posts/[id].tsxtsx
pages/posts/[id].tsxtsx
Note that the default behaviour of react-query is to refetch the data on the client-side when it mounts, so if you want to only fetch the data via getStaticProps, you need to set refetchOnMount and refetchOnWindowFocus to false in the query options.
This might be preferable if you want to minimize the number of requests to your API, which might be necessary if you're using a third-party rate-limited API for example.
This can be done per query:
tsx
tsx
Or globally, if every query across your app should behave the same way:
utils/trpc.tstsx
utils/trpc.tstsx
Be careful with this approach if your app has a mixture of static and dynamic queries.