#Server Actions - revalidation

8 messages · Page 1 of 1 (latest)

sonic pebble
#

Hello,

I'm trying to migrate from API routes to server actions. In my case I have a button to delete some stuff and onSuccess display a toast message, in server actions I call revalidatePath() (I'm fetching data from external service - out of next.js) and everything is fine, but my toast appears before revalidation is completed (isPending is still true). Is there an option to wait for the revalidation and display toast after it?

Here's my code:


  const [isPending, startTransition] = useTransition()

  const clickHandler = () => {
    startTransition(async () => {
      const res = await deleteGroup(groupId)
 
      if (!res.ok) {
        errorToast(res.error)
        return
      }

      successToast('Group removed')
    })
  }

server action:


export const deleteGroup = async (groupId: string) => {
  const res = await fetch(`${URL}/${groupId}`, {
    method: 'DELETE',
  })

  if (!res.ok) {
    return {
      ok: false,
      error: 'Failed to delete group. Please try again.',
    }
  }

  revalidateTag('groups')

  return {
    ok: true,
    data: 'Group deleted successfully.',
  }
}

jovial lindenBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

vivid plinth
#

isPending is not tied to your revalidation but to the server action as a whole

#

I think isPending is expected to be true until the function in your transition is not done

#

so isPending being true is not an indicator of the revaliation being done or not

#

I am not sure what you mean by waiting the revalidation to be done

sonic pebble
sonic pebble