Next.js Discord

Discord Forum

Data fetching and server-actions aborting

Unanswered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
I'm building an app that has separated backend and frontend. On the frontend side I created a wrapper for the featch api to do all kind of preparation and data parsing and marked it as server-only. Then in the separate file
requests.ts
I call "use server" at the top and then create async functions that are the api calls to the backend endpoints.
I'm trying to add aborting requests capability to this scheme, but it seems that i'm missing something. Because when the fetch is on the server side and all the requests are done via "use server", then I can't prevent them from beeing ran, even though the signal is sent. I have some pages that use page.tsx and requests are done at that time, I also have some pages that use custom query hook to call requests on a client side, but nothing of that works if the fetch is on the server. If I remove the "use server" and "server-only", then I have all my api secrets revelaed to client as well all requests are seen in the network tab with all credentials. How can I still use fetch completely on a server side and still benefit from AbortController to cancel duplicated requests and cancel subsequent requests for the same url, but with different params?

0 Replies