Confusion about route handler and server action
Answered
American black bear posted this in #help-forum
American black bearOP
I have finished 4 projects with next.js, however, I am more and more confusion that When would I use route handler rather than server action, and when would I use server action rather than route handler.
usually, base on the latest next.js, we have more than 3 methods to fetch data from api server:
1. directly to visit service api.
2. by using route handler and visiting service api with route handler in server side.
3. by using server action to visit service api.
I think it's very simple to use server action to fetch data from api server. however, what's the meaning of the 1 and 2 method exist in next.js framework?
usually, base on the latest next.js, we have more than 3 methods to fetch data from api server:
1. directly to visit service api.
2. by using route handler and visiting service api with route handler in server side.
3. by using server action to visit service api.
I think it's very simple to use server action to fetch data from api server. however, what's the meaning of the 1 and 2 method exist in next.js framework?
Answered by B33fb0n3
yea, don't use a server action for fetching data. Server actions exists to do mutations from client. Do this: https://nextjs-forum.com/post/1252579715659595857#message-1252593364839432223
15 Replies
@American black bear I have finished 4 projects with next.js, however, I am more and more confusion that When would I use route handler rather than server action, and when would I use server action rather than route handler.
usually, base on the latest next.js, we have more than 3 methods to fetch data from api server:
1. directly to visit service api.
2. by using route handler and visiting service api with route handler in server side.
3. by using server action to visit service api.
I think it's very simple to use server action to fetch data from api server. however, what's the meaning of the 1 and 2 method exist in next.js framework?
Route handlers are pretty nice, if you need external api access. Like that you can build a backend for others, that they can use.
As you said: server actions can be used to remove the complicated route handlers, because server actions are easy to setup, but can't be externally used.
Calling a method inside server side code is the most common approach for me, when it comes to data fetching
As you said: server actions can be used to remove the complicated route handlers, because server actions are easy to setup, but can't be externally used.
Calling a method inside server side code is the most common approach for me, when it comes to data fetching
@B33fb0n3 Route handlers are pretty nice, if you need external api access. Like that you can build a backend for others, that they can use.
As you said: server actions can be used to remove the *complicated* route handlers, because server actions are easy to setup, but can't be externally used.
Calling a method inside server side code is the most common approach for me, when it comes to data fetching
American black bearOP
by using route handler to fetch data, sometime, just do the same things again in the handler as what we do in client side; for example,
In client component:
const AComponent: React.FC = ()=> {
useEffect(()=> {
fetch('api/text', {}).then(()=> {}).catch((err)=>{});
}, []);
}
In the api/test directory:
const POST = async()=> {
fetch(apiEndPoint, {}).then(()=> {}).catch((err)=> {});
}
base on the code above, two parts of code almost the same.
however, if we use server action directly in client component.
const AComponent: React.FC = ()=> {
useEffect(()=> {
fetch('apiEndPoint', {}).then(()=> {}).catch((err)=>{});
}, []);
}
just type one time code and fetch the data.
so, base both the two methods, I am confusion how do I make a decision what option would we choose, whether server action or route handler or not
In client component:
const AComponent: React.FC = ()=> {
useEffect(()=> {
fetch('api/text', {}).then(()=> {}).catch((err)=>{});
}, []);
}
In the api/test directory:
const POST = async()=> {
fetch(apiEndPoint, {}).then(()=> {}).catch((err)=> {});
}
base on the code above, two parts of code almost the same.
however, if we use server action directly in client component.
const AComponent: React.FC = ()=> {
useEffect(()=> {
fetch('apiEndPoint', {}).then(()=> {}).catch((err)=>{});
}, []);
}
just type one time code and fetch the data.
so, base both the two methods, I am confusion how do I make a decision what option would we choose, whether server action or route handler or not
@B33fb0n3 Do you need the endpoint to have external access?
American black bearOP
the apiEndPoint is the backend api service which can visit database, some foundation service, however it may visit the exteral partner's service
@B33fb0n3 So, does your endpoint (this: see attached) need external access?
American black bearOP
I think, No! it is hidden behind the server. it's a backend service.
Thanks for the clear response. If you don't need external access, why do you want to create an endpoint, that is externally reachable.
But: we also need to consider, where you fetch your data. You are fetching it inside a client component. Fetching inside a client component is ok, but you might want to pass it from a server component directly down, to use server side fetching. Or if you don't want to use serverside fetching, you should use a client side library for fetching data like react query or swr.
So fetch data serverside and pass down as props. That's not possible no worries at all: get a client library and build your endpoint for data fetching. Fetch your own endpoint and the endpoint fetches from your external source.
If it's possible, you might want to fetch the external source directly from your client
But: we also need to consider, where you fetch your data. You are fetching it inside a client component. Fetching inside a client component is ok, but you might want to pass it from a server component directly down, to use server side fetching. Or if you don't want to use serverside fetching, you should use a client side library for fetching data like react query or swr.
So fetch data serverside and pass down as props. That's not possible no worries at all: get a client library and build your endpoint for data fetching. Fetch your own endpoint and the endpoint fetches from your external source.
If it's possible, you might want to fetch the external source directly from your client
@American black bear I just draw a archtecture diagram to describe it.
yea, don't use a server action for fetching data. Server actions exists to do mutations from client. Do this: https://nextjs-forum.com/post/1252579715659595857#message-1252593364839432223
Answer
@B33fb0n3 yea, don't use a server action for fetching data. Server actions exists to do mutations from client. Do this: https://discord.com/channels/752553802359505017/1252579715659595857/1252593364839432223
American black bearOP
Thank your for your kindly response, I am confusion about the mutations, how do I understand the words: mutations from client. Thanks again.
@American black bear Thank your for your kindly response, I am confusion about the mutations, how do I understand the words: mutations from client. Thanks again.
Imagine you build a ecommerce store and you handle your cart via your database. Then the client want to add something to his cart. The client clicks the button "add to cart". Now the client starts a mutation of the cart objects and mutates it correctly. It's initiated clientside. That's a mutation from the client
@American black bear got,Thanks
sure thing. Is your question solved like that?
@joulev