Progressively Enhanced Mutations with Next.js Server Actions
Next.js is a popular framework for building server-side rendered React applications. One of its key features is the ability to use server-side actions to handle mutations in a progressively enhanced way.
What are Server Actions?
In Next.js, server actions are functions that run on the server before the page is rendered. They can be used to handle mutations, such as updating a database, sending an email, or any other server-side operation.
Progressively Enhancing Mutations
Progressive enhancement is the concept of starting with a basic, functional version of a feature and then adding more advanced functionality as the user’s capabilities allow. With Next.js server actions, we can apply this concept to mutations.
For example, let’s say we have a form that allows users to submit a new post. Instead of immediately sending the form data to the server and updating the database, we can start by using a client-side mutation to update the UI with the new post. Then, in the background, we can use a server action to update the database.
Benefits of Progressively Enhanced Mutations
Progressively enhanced mutations have several benefits:
- Improved Performance: By handling mutations on the client side first, we can provide a faster and more responsive user experience.
- Resilient to Network Failures: If the server action fails for any reason, the client-side mutation has already updated the UI, preventing a negative user experience.
- Better User Experience: Users can see the immediate impact of their actions, even before the server has processed the mutation.
Using Server Actions with Next.js
To use server actions with Next.js, we can define a special function called getServerSideProps
in our page component. This function runs on the server and can be used to handle mutations or other server-side operations.
Here’s an example of using getServerSideProps
to handle a mutation:
export async function getServerSideProps(context) {
// perform server-side operation (e.g. update database)
await updateDatabase(context.req.body);
// return updated data to be used in the page
return {
props: {
data: updatedData
}
};
}
By using getServerSideProps
in this way, we can progressively enhance mutations and provide a better user experience while still taking advantage of server-side rendering and the benefits it provides.
🚀 UPDATED VERSION: https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF 🚀
The Supabase Next.js Tailwind CSS and Typescript template doesn’t include components in the tailwind config. Tailwind CSS site has an updated config handling components, app, etc.. so I figured you guys might update it. I just assumed the config had the updated one from tailwind and spent a while figuring out why my tailwind wasn’t working in components folder 😂
STOP SWITCHING BACK AND FORTH BETWEEN SCREEN AND YOUR FACE !!! THAT'S IRRITATING. JUST KEEP THE SCREEN
I'd like to implement upvotes functionality using optimistic updates, that is much cooler 😎
Thank you! 🎉❤
server actions is awesome waiting for the stable version !
Useful tutorial. Thank you.
This is an amazing tutorial! So much fun to follow!
FYI: this video would be much more enjoyable without you acting like a clown.
This is so wild
Whats with the page flashing / moving when you click increment @15:30 ? Thats not ideal for a nice user experience. I noticed the heading of "Readings" is also moving.
To check if it's really progressively enhanced, turn off JS and see if the app still works. Doesn't look like it to me.
It's easier to develop server-side HTML or server-side JSX (as server-side templates) by sending JS down the wire to intercept form submission than all these "user server" declarations with client-side and server-side code everywhere. Looks nice in a small application, but once you get to some significant size the leaky abstractions start to creep in and now you have one more thing to debug. Features take years to develop and then everyone rewrites again.
Programmers have learned that this kind of monolithic frameworks doesn't work 10-20 years ago and we keep making the exact same mistakes, just with a new buzzword.
Could you expand upon this to show how to show them all but only those authenticated will see the submit form? Struggling with auth tying into this stuff
Arc power user
When I enable server actions vercel spits build errors. Compiles fine if I disable them
This is incredible! Server actions but without having to reload the page
Is there more info on use cases involving rpc with supabase?
Great Content … Thank you … But i feel somehow dizzy because of how you are constantly moving your hands and head… maybe it's just me 🙂… Still great content
What browser is that?
Awesome. More like this please.
What? Are you telling we don't actually need APIs any more?