Replies: 1 comment
-
The React docs talk about this, https://react.dev/reference/rsc/use-server:
And while, it is just fine, like, clearly it works for you, it is not necessarily ideal. You could make a Route Handler, that does this. One could argue, that the route would be publicly reachable, but so is your data when used, let's say, through react-query. You probably want to still limit access to same origin and such in your Route Handler though. Also, that these are processed in a queue is probably not ideal. Like, if you have many server actions queued, and your fetching data action is enqueued, it'd have to wait for its turn. I guess, you could move into a pattern where each page is a url search param, like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I have a use case where I need to do some client-side data fetching using TanStack Query — for example:
The issue is:
My backend API requires an access token that is only available on the server side (e.g., retrieved via auth() from auth.js).
To work around this, I’ve been using a trick: placing 'use server' at the top of a function inside my Data Access Layer (DAL), which allows me to get the token from the server, then pass it to the client.
However, I noticed in the docs that Server Actions are intended for mutations, and it doesn't mention anything about using them just to fetch data.
My questions:
Thanks in advance!
Additional information
dal/getSomeData.ts
hooks/useInfiniteGetSomeData.ts
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions