Oct 10, 2023
With the release of Next.js 13.4, an exciting new feature called server actions has been introduced, enabling the combination of client-side interactivity and server-side data fetching. Server Actions are an alpha feature in Next.js, built on top of React Actions. They enable server-side data mutations, reduced client-side JavaScript, and progressively enhanced forms. They can be defined inside Server Components and/or called from Client Components:
"The React ecosystem has seen a lot of innovation and exploration of ideas around forms, managing form state, and caching and revalidating of data. Over time, React has become more opinionated about some of these patterns. For example, recommended “uncontrolled components” for form state.
The current ecosystem of solutions has either been reusable client-side solutions or primitives built into frameworks. Until now, there hasn't been a way to compose server mutations and data primitives. The React team has been working on a first-party solution for mutations.
We're excited to announce support for experimental Server Actions in Next.js, enabling you to mutate data on the server, calling functions directly without needing to create an in-between API layer."
To be able to use Server Actions in your Next.js project you have to enable the experimental serverActions flag.
Server Actions can be defined in two places:
In the development of my ecommerce website BeatsXchange, I encountered a problem when implementing the cart functionality: I needed to have access to the prismaClient and use callbacks in the same component. Specifically, I had to implement the addToCart functionality in app/[product]/page.tsx and the resetCart functionality in the app/cart/page.tsx and have the cart page being refreshed everytime one of this actions were executed.
Both routes use the <Button/>
component, which faced limitations when trying to pass event handlers as props.
I tried to fidget with this approach and make it work but I had two problems that constantly came up:
As I searched for a solution for this issue I discovered Server Actions. By transforming the functions into server actions (by adding the 'use server' directive at the top of the body) and then passing them and their parameters to the <Button/> component I resolved the issue of passing event handlers as props ERROR: Event handlers cannot be passed to Client Component props
. Additionally, by utilizing the useTransition() hook in the client component and revalidatePath('/cart') in the server actions, the cart page would now be automatically refreshed with the updated content.
When calling Server Actions from a different page, revalidatePath('/cart') only worked the first time and did not refresh the cart page in subsequent calls. This is a known issue that is currently being fixed. As a temporary workaround, I accessed the cart page using the old anchor instead of using the next/Link component.
Server actions allow for server-side data mutations, reduced client-side JavaScript, and enhanced form management. By enabling server actions, you can perform server-side tasks without an API endpoint and integrate those with client specific interactivity.
Although there are a few advantages compared to using API endpoints, like being able to integrate multiple platforms with the same API, server actions can definetely be the best option in some instances like the one above.
It's important to note that server actions are currently in an alpha stage, which means there is ongoing development and refinement happening. Although they are not yet stable, the potential for improvement and the addition of new features is something to be excited about.
If you're interested, you can explore the code for the project mentioned above on GitHub. Additionally, feel free to connect with me on LinkedIn. And remember, a smile is the best error handler.
< BACK