Next.js Discord

Discord Forum

NextJs 14 revalidatePath not working as expected

Unanswered
Maine Coon posted this in #help-forum
Open in Discord
Maine CoonOP
Hi all, i'm using revalidatePath() from next/cache to refresh the current page so that new changes could reflect but it is not working as expected. Below is the code I have:-
app router structure:- app/user/[id]/page.tsx
server component
const getUserData = async (id) => {
  // get user data logic
}
const User = async ({params}) => {
  const userData = await getUserData(params.id)
  return <UserProfile data={userData} />
}

client component
const UserProfile = ({data}) => {
  const sendReq = async () => {
    const res = await fetch("/api/user/send-request");
    const resData = await res.json();
    if (!res.ok) throw new Error();
    revalidatePath(`/user/${data._id}`) // not working
  }
  return (...)
}

So i want to refresh the current page right after the sending request so that i could see latest data changes...

Can someone guide?

21 Replies

Toyger
I don't see any data variable in your UserProfile component, but you are using it in revalidatePath
@Toyger I don't see any `data` variable in your `UserProfile` component, but you are using it in revalidatePath
Maine CoonOP
updated the code, its there already... just forgot to write here
@Maine Coon updated the code, its there already... just forgot to write here
Toyger
ok you revalidate it, but do you redirect again on same page, or at least update it with useRouter reload after revalidate?
im not sure how to do that..
@Maine Coon im not sure how to do that..
Toyger
import { redirect } from 'next/navigation'
.....
revalidatePath(`/user/${data._id}`)
redirect(`/user/${data._id}`)
@Toyger js import { redirect } from 'next/navigation' ..... revalidatePath(`/user/${data._id}`) redirect(`/user/${data._id}`)
Maine CoonOP
ohh, i thought by using router.refresh or something like that, i also read about window.location.reload()...
@Maine Coon ohh, i thought by using `router.refresh` or something like that, i also read about `window.location.reload()`...
Toyger
you can try any of that, but I think redirect is more robust way
@Toyger you can try any of that, but I think redirect is more robust way
Maine CoonOP
will try, thanks!!
@Toyger you can try any of that, but I think redirect is more robust way
Maine CoonOP
didn't work...
@Maine Coon didn't work...
Toyger
should be fine, are you sure that data changed in db?
you can also try add to your route/page this
export const dynamic = 'force-dynamic'
@Toyger should be fine, are you sure that data changed in db? you can also try add to your route/page this js export const dynamic = 'force-dynamic'
Maine CoonOP
yes data changed, when i manually refresh the page, i can see the changes
nope...
had to manually refresh the page, then i see the changes as usual..
@Maine Coon had to manually refresh the page, then i see the changes as usual..
Toyger
but data changes, so revalidatePath works fine.
did you tried all ways to refresh? like redirect, userouter.reload, window.location.reload
@Maine Coon yes... none of them works
Toyger
that's really strange, try in other browser, ideally with incognito mode
because if manual refresh keep page updated then any other way should too.
@Maine Coon didn't work in incognito too, but manual refresh works... 😥
Toyger
can you share repo, because I have no other ideas, it's easier to test it directly
@Toyger can you share repo, because I have no other ideas, it's easier to test it directly
Maine CoonOP
actually its a private repo... i'll try to create a small reproducible example as current project has quite a lot...