Advancements in Mutations through Next.js Server Actions

Posted by

Progressively Enhanced Mutations with Next.js Server Actions

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.

0 0 votes
Article Rating
24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Supabase
7 months ago
@groverrl
7 months ago

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 😂

@ratmirgasimov1958
7 months ago

STOP SWITCHING BACK AND FORTH BETWEEN SCREEN AND YOUR FACE !!! THAT'S IRRITATING. JUST KEEP THE SCREEN

@user-hb6yb6bu8n
7 months ago

I'd like to implement upvotes functionality using optimistic updates, that is much cooler 😎

@artems5802
7 months ago

Thank you! 🎉❤

@nested9301
7 months ago

server actions is awesome waiting for the stable version !

@joshuajaydan
7 months ago

Useful tutorial. Thank you.

@AndrewAkaHrun
7 months ago

This is an amazing tutorial! So much fun to follow!

@mvtro
7 months ago

FYI: this video would be much more enjoyable without you acting like a clown.

@realitydesigners
7 months ago

This is so wild

@zugbob
7 months ago

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.

@FagnerBrack
7 months ago

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.

@brianwalsh7040
7 months ago

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

@johnpeterson8493
7 months ago

Arc power user

@IanJamieson
7 months ago

When I enable server actions vercel spits build errors. Compiles fine if I disable them

@IanMathaiya
7 months ago

This is incredible! Server actions but without having to reload the page

Is there more info on use cases involving rpc with supabase?

@khaledsanny4817
7 months ago

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

@diogenes5795
7 months ago

What browser is that?

@mitchlabrador
7 months ago

Awesome. More like this please.

@serhiimamedov
7 months ago

What? Are you telling we don't actually need APIs any more?