server action not running server side
Unanswered
West African Lion posted this in #help-forum
West African LionOP
Hello! I'm currently trying to have my website access an api that has cors enabled, so I need to go through a server side function. For now, I'm trying to see if it'll work to literally just forward the request, and then I'll add more security in the future. This is the function I have right now, in a file ''FetchProxy.js":
When I try to run it in a component in another file, it runs client side, logging hi to the client console and trying (and failing) to run the fetch client side. Is there any reason why something would make it not run server side?
Thanks!
'use server';
export async function fetchFromServer(url, options) {
console.log('hi!');
return await fetch(url, options);
}
When I try to run it in a component in another file, it runs client side, logging hi to the client console and trying (and failing) to run the fetch client side. Is there any reason why something would make it not run server side?
Thanks!
3 Replies
Short mackerel
Server actions are usually done to POST data, to mutate something. Not to GET something from the server.
If you want to fetch data from the backend, you would need to fetch in a server component and pass the data into a client component. Or if it's highly dynamic, then you. can fetch data in API routes.
If you want to fetch data from the backend, you would need to fetch in a server component and pass the data into a client component. Or if it's highly dynamic, then you. can fetch data in API routes.
American
Are you sure it's not because of this?