#ServerActions are serialized?

1 messages · Page 1 of 1 (latest)

ember thicket
#

I mean if I call multiple times a long time server action, they will wait each others and call one be one synchronously in the browser?

hasty blazeBOT
#

Hi @ember thicket. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Server Actions in Next.js are asynchronous functions that are executed on the server. They can be used in both Server and Client Components to handle form submissions and data mutations in Next.js applications. However, the documentation does not specify whether multiple calls to a long time server action will be queued and executed one by one synchronously in the browser.

In general, asynchronous functions in JavaScript do not wait for each other unless you explicitly code them to do so using constructs like async/await or Promises. If you call an asynchronous function multiple times without waiting for the previous call to complete, the calls will be made concurrently, not sequentially.

However, it's important to note that Server Actions in Next.js have some specific behaviors:

  • In Client Components, forms invoking Server Actions will queue submissions if JavaScript isn't loaded yet, prioritizing client hydration.
  • After hydration, the browser does not refresh on form submission.
  • Server Actions are not limited to <form> and can be invoked from event handlers, useEffect, third-party libraries, and other form elements like <button>.
  • Server Actions integrate with the Next.js caching and revalidation architecture. When an action is invoked, Next.js can return both the updated UI and new data in a single server roundtrip.

For more details, you can refer to the Next.js documentation on Server Actions (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations).

#

If you need to ensure that multiple calls to a Server Action are executed sequentially, you may need to manage that within your own code, possibly by using some form of state to track whether a Server Action is currently being executed and then waiting for it to complete before starting the next one.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: