Next.js Discord

Discord Forum

2 questions about server actions.

Answered
Pont-Audemer Spaniel posted this in #help-forum
Open in Discord
Pont-Audemer SpanielOP
Hi, I am currently using trpc and found server actions really interesting but I have 2 question.
1. Would this be able to replace something like trpc for smaller or even maybe bigger apps ?
2. Let's say I want to fetch a db using prisma how can I safely import the db ?
Answered by fuma
1. No, it won’t be as convenient as TRPC. Server Action isn’t supposed to do data fetching, but form mutations. You have to manually wrap it in React Query or SWR to get caching ability. And it’s currently recommended to use fetch + Route Handlers for data fetching

2. Make sure you are only referencing it in server side code, Next.js bundler will handle it well.

Notice that Server Action is an experimental feature that shouldn't be used in production
View full answer

9 Replies

1. No, it won’t be as convenient as TRPC. Server Action isn’t supposed to do data fetching, but form mutations. You have to manually wrap it in React Query or SWR to get caching ability. And it’s currently recommended to use fetch + Route Handlers for data fetching

2. Make sure you are only referencing it in server side code, Next.js bundler will handle it well.

Notice that Server Action is an experimental feature that shouldn't be used in production
Answer
Server side caching is a bit different from client side caching
Redis also come with a cost while client side cache doesn’t
And you will need client side caching for advanced revalidation logic
@fuma Server side caching is a bit different from client side caching
Pont-Audemer SpanielOP
I mean I feel like for things like fetching a users servers it would be better or is TRPC just better all around
Server action for data fetching is likely an anti-pattern (like fetching data with POST requests). TRPC still worth considering if you want to enjoy the benefits from their React Query wrapper and some convenient features.

However, I personally prefer directly use fetch + RQ/SWR, shared types, middleware, you can easily write your own wrapper around route handler, it is much easier than you think
Surely Server action won’t fully replace TRPC, mutation is just part of its feature
For mutations? Yes, server action is nice (when it’s a stable API)