Server actions are for mutation, what can I use for fetching data?
Answered
Sun bear posted this in #help-forum
Sun bearOP
In the nextJS docs it says server actions are basically for form submissions:
Though it can be used for fetching data. My question is if the docs dont say its for fetching, then what could I use to async fetch data on the server side?
Do I have to use an api route?
Use case: I have a supabase instance which needs admin rights to fetch some data async and so has to be done on the server.
Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications.Though it can be used for fetching data. My question is if the docs dont say its for fetching, then what could I use to async fetch data on the server side?
Do I have to use an api route?
Use case: I have a supabase instance which needs admin rights to fetch some data async and so has to be done on the server.
Answered by B33fb0n3
nah, not clientside. If you need data clientside you normaly pass it from a server component to the client component and fetch it serverside. If you can't do that, you technically can use server actions. However there are some disadvantages with it. Fetching data clientside should be done via api routes and a clientside fetching library like react query or swr to have cache and other useful stuff
7 Replies
@Sun bear In the nextJS docs it says server actions are basically for form submissions:
Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications.
Though it can be used for fetching data. My question is if the docs dont say its for fetching, then what could I use to async fetch data on the server side?
Do I have to use an api route?
Use case: I have a supabase instance which needs admin rights to fetch some data async and so has to be done on the server.
For fetching data serverside you can directly call your supabase instance and fetch with it
Sun bearOP
client side? not when you need supabase admin access as you shouldnt expose your admin key
@Sun bear client side? not when you need supabase admin access as you shouldnt expose your admin key
nah, not clientside. If you need data clientside you normaly pass it from a server component to the client component and fetch it serverside. If you can't do that, you technically can use server actions. However there are some disadvantages with it. Fetching data clientside should be done via api routes and a clientside fetching library like react query or swr to have cache and other useful stuff
Answer
Sun bearOP
yeah ok so i guess api endpoints is a the recommended way to go, and not server actions
@Sun bear yeah ok so i guess api endpoints is a the recommended way to go, and not server actions
yes, api endpoints are the way to go and don't forget to use a clientside fetching library ^^
Sun bearOP
alright thats for clarifying