tRPC and REST
We're considering building tRPC Nextjs base systems which pull data from Strapi via REST calls. So, instead of using getServerSideProps, I am thinking we will would create a router and define procedures which each make appropriate REST calls to Strapi.
Is this the right approach? Your comments and thoughts would be appreciated.
Thanks!
10 Replies
yes, using tRPC as a "backend-for-frontend" makes sense
bonus points if you validate the response from strapi with zod before sending it back to the client
Do you want to do SSR/prerendering or is fetching client-side enough?
Are you going to be typing your responses from Strapi?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
getServerSideProps also runs on the server
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
The only benefit of doing it without SSR is that you get strong types - but if the data coming from Strapi isn't typed, then it's pointless
Thanks for the responses. SSR is would be nice as would strong types. I've got this largely working, however, has anyone a good example of REST gets from a tRPC router with typing/response validation?
So if I want my site have good SEO I should use ssr: true? Didn't understand the strong types part, I did some testing and would this be good and strongly typed endpoint for client to consume? I am trying to understand would it be good to use trpc on project where I need to get data from headless cms / external api for example and hide my api key. If I need good SEO I should use ssr with trpc?
And would this be basically the same with only using react query? So if I just need couple of endpoints to hide my api key is it worth to use trpc for it instead of creating couple normal api endpoints and parsing data there also?
My api endpoint that I could consume using react query like above:
@rocawear thanks for this, much appreciated and really helpful. I've been working with plain old fetch(). Your code uses axios. I'm just wondering (noob!) what's the primary reason you have used axios over plain fetch ?
habbit, its does not matter what you use. I think axios throws error but fetch does not
i think axios is more mature for production because it has more features