On Redirection Data is not getting refreshed | Next JS 13
Unanswered
gauravlogical posted this in #help-forum
I am new to NextJS 13 world, I have created the following components for TODO.
1. Get TODO page, where you can get all of the todo via
2. Create Todo page, which is calling a server action on creating a new todo and redirects user to the Get TODO page.
On redirection, the data is not refreshed. I tried
Please explain me how to tackle this basic problem.
1. Get TODO page, where you can get all of the todo via
async function getTodos() {
return await prisma.todo.findMany({
orderBy: {
created_at: "desc",
},
});
}2. Create Todo page, which is calling a server action on creating a new todo and redirects user to the Get TODO page.
async function createTodo(formData) {
"use server";
await prisma.todo.create({
data: {
todo: formData.get("todo"),
is_completed: false,
},
});
redirect('/')
}On redirection, the data is not refreshed. I tried
revalidatePath as well but no luck.Please explain me how to tackle this basic problem.
9 Replies
Because Next.js has a router cache. Previous pages will cached by the client (last for 30 seconds), and currently you can’t opt out of it.
A common workaround is to use
A common workaround is to use
router.refresh, or perform the fetch client-sideYea, I missed that
I have already tried adding that line in file like this -> Since create is on
/create and get all todos are on /, So I tried adding revalidatePath('/') in create directory page.jsx but Its still not working 😢European sprat
It's going to be filled with issues and unexpected results unless you just do a client side fetch with something like react-query.
Hopefully there's an update from the Vercel team today so we can know what the plan is for it
Disappointed with that sometimes, that’s why I alway recommend client-side fetching if you are struggling with the caching
One thing worked that is adding one link without prefetch ->
<Link href='/' prefetch={false}> . But as of now nothing works programmatically. I agree revalidatePath ideally should work before redirect.