Server Actions - revalidation
Unanswered
Bigheaded ant posted this in #help-forum
Bigheaded antOP
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:
server action:
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.',
}
}7 Replies
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
@Eric Burel I am not sure what you mean by waiting the revalidation to be done
Bigheaded antOP
Later I’ll try to record what I mean by that
Bigheaded antOP
Toast appeared before server component got new data and I want to display toast after isPending will be again false.