Is it possible to send a response body/object when also revalidating a path inside Server Action in Next.js?
This is my Server Action function:
export async function addTodo(prevState: any, formData: FormData) { const errors: { title?: string; description?: string; } = {}; const title = formData.get('title')?.toString(); const description = formData.get('description')?.toString(); // Validate title and description if(errors.title || errors.description) { return { errors }; } await dbConnect(); const todo = new Todo({ title, description }); await todo.save(); revalidatePath('/'); return { todo: { title, description } };}
Sometimes my formState
object which is returned by useFormState
hook has todo
property after processing the request successfully and sometimes it doesn't. So it doesn't seem to be working reliably.