when to use tRPC vs a nextjs api route
ive really started to appreciate the tRPC-prisma data flow to my frontend, but im coming from a nextjs-not-typescript background. ive been using api routes there forever, and i still see it in the ct3a directory. but when do i use it then?
19 Replies
If your backend isn't typescript, you wouldn't want to use trpc
Oh, I think I read that wrong 😄
this would be for a new project im starting for scratch!
You should really use it for anything that doesn't need to be accessed by an external service/app
You could even use it for that too with trpc-openapi
so in my mind tRPC is the replacement of nextjs api routes with the benefit of typesafety. is that correct or do i need to reframe it?
Essentially yes!
And it includes all the nice react query stuff automatically
yep, its great how its RQ under the hood
but i really miss RQ devtools, i dont like having my browser's console bloated with these blue query #xx messages 😦
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oh that's true, i did see that file and forgot about it!
Yes, thanks for pointing that out
quick q, @Pod if im calling the same trpc api endpoint on one page, say /pages/fileA.tsx and i need to access the same endpoint for the same data on /pages/fileB.tsx, is there a best practice on sharing the data without making an extra/unecessary api call?
You could do something like a simple zustand store, or tweak cache settings
will check out zustand, ty
You can use RQ devtools
The data will be cached, so you’re not making an additional api request
This is a standard React Query feature
woah, didnt know that. is there a way to check if the data returned is fresh from my db vs just from cache?
Check if you’re making network requests
How RQ behaves depends on staletime and cachetime but just play around with it a bit
will do, so no need for a zustand store then?
Not for this specific purpose
Webhooks will need their own dedicated api routes still
Unless you have specific middleware to differentiate a webhook from a normal trpc call (which is more effort with minimal benefits)
I still use zod to validate webhook event payloads tho
oh yea i see now and that youre the same thread too
for whoever comes here: https://github.com/t3-oss/create-t3-app/issues/329 and comment out/remove
loggerLink
in /utils/api.ts
by this https://trpc.io/docs/links/loggerLinkGitHub
feat: add ReactQueryDevtools to the project by default · Issue #329...
Is your feature request related to a problem? Please describe. ReactQueryDevtools is so helpful that I believe this should be a standard in the t3 stack, it's much more common to need it than d...
Logger Link | tRPC
loggerLink is a link that lets you implement a logger for your tRPC client. It allows you to see more clearly what operations are queries, mutations, or subscriptions, their requests, and responses. The link, by default, prints a prettified log to the browser's console. However, you can customize the logging behavior and the way it prints to the...