Next.js Discord

Discord Forum

revalidatePath function does not working when I create a new task using a dynamic url

Unanswered
Yacare Caiman posted this in #help-forum
Open in Discord
Yacare CaimanOP
Hi, I am currently building on task management application using NextJs 14. When I create a new task, the UI does not show the new task added until I have refreshed the page. I have tried using revalidatePath function but it does not work.

Here is what my code looks like

export async function createTask(
columnId: number,
prevState: FormState,
data: FormData
): Promise<FormState> {
const formData = formDataToJson(data);
const parsed = schema.safeParse(formData);
if (!parsed.success) {
return {
message: "Invalid form data",
};
}

const tasks = await prisma.task.findMany();

// Create a new task
const task = await prisma.task.create({
data: {
title: formData.title,
description: formData.description,
subtasks: { create: formData.subtasks },
status: formData.status,
columnId: tasks.find((item) => item.status === formData.status)
?.columnId as number,
},
});

// Optionally, update the column to include the new task
const updateColumn = await prisma.column.update({
where: {
id: tasks.find((item) => item.status === formData.status)
?.columnId as number,
},
data: {
tasks: { connect: { id: task.id } },
},
});
revalidatePath(/board/[${columnId}], "page");
return { message: "New task created" };
}

Here's a link to the repo: https://github.com/mrvicthor/task-manager-app
Please, any ideas on how i can resolve this?

Thanks

7 Replies

Sun bear
Hi,

you have to remove the brackets in revalidatePath.

revalidatePath(/board/${columnId})
Yacare CaimanOP
@Sun bear thanks for your feedback. i will give it a try
unfortunately, it didn't work
@Yacare Caiman unfortunately, it didn't work
Sun bear
You can try to remove the , "page" parameter. Maybe that could help
Yacare CaimanOP
I have done that as well
Sun bear
Oh okay. So the easiest fixes are gone 😄 I guess you have to log a lot of data to the console while running the function again and again.

Eg Check if relevant data for this route is really changing.

Sorry at this point and hope you will be able to solve it fast
Yacare CaimanOP
yea, the data actually changes.