Server actions to READ data (vs WRITE) #58674
Replies: 4 comments 3 replies
-
|
100% agree, have exact same thoughts |
Beta Was this translation helpful? Give feedback.
-
|
@nikitastryuk thanks! You may find the following video interesting: https://www.youtube.com/watch?v=IFYFezylQlI There, the author implements an infinite scroll where initial data loading is performed on the BE, and the consequent fetches are performed on the FE (via a server action). One commenter even asks the same question as we have: "Is the approach legit and (if so) why it's not documented properly?" The answer was that "I think it's legit but, you know, it's all experimental, bro..." I've also messed with this in sandboxes and what I can confirm – there're definitely pitfalls with caching. As soon as you need to combine non-interactive server-side with interactive client-side, working with the same data. Overly aggressive NextJS caching interferes with everything. The simplest example. If you have an infinite scroll with a server-side initial load – you have to |
Beta Was this translation helpful? Give feedback.
-
|
Leerob made an interesting commit to NextJS repo yesterday: Quietly removing the recommendation that Server Actions can be used for Data Fetching :( If Vercel are shifting the narrative to SA are only for mutations not fetching, then continuing to use SA for data fetching feels like you are swimming against the tide and it will essentially becomes unsupported... |
Beta Was this translation helpful? Give feedback.
-
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.
-
So I've checked all this new NextJS stuff with RSC, Router Handlers and Server Actions and there's one thing I fundamentally do not get 🤔
Server Actions are presented as an alternative to API routes. It's an implementation of an old RPC idea explored in TRPC, Telefunc and Zodios. I understand and share the appeal to replace API routes with functions. Afterall, a function is the most straigtforward to describe a contract between a producer and a consumer (of some functionality).
There're some rough edges that currently make the approach less convenient than it could be. But such issues will be fixed in the future – I guess.
Hovewer, what I find missing in the context of Server Action is the read (vs write) part of a data flow. From the docs it seems like we are proposed to implement Router Handlers which are, still... HTTP/API endpoints – that were supposed to become unnecessary.
I realize that fetching data on the server should be preferred, but it's not always an option (infinite scrolls, etc. interaction-triggered fetching). So let's assume we're talking about interactive, client components.
To read data we should
fetchfrom an HTTP endpoint declared e.g in aapp/api/route.tsfile, in one folder, with one kind of syntax and conventions. And to write data we should call a server action, declared e.g inactions/index.tsfile in another folder with another kind of syntax and conventions.Am I the only one who's surprised by it, and finds it counter-intutive & inconsistent?
There's an API to directly call a Server Action which can be used to fetch data on the client:
Hovewer, the above is not compatible with cache & invalidations, unlike a
fetchfunction 🤷♂️I'm not excited by the necessity to have 2 very different and naturally incompatible approaches to connect a client and a server. If we go the action road, wouldn't it be natural to use them for ALL client-server communications, at least by default. Do I miss something?
Beta Was this translation helpful? Give feedback.
All reactions