next.js: [NEXT-1128] Client-side cache is not revalidated when calling revalidatePath + redirect in server action
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2
Binaries:
Node: 18.16.0
npm: 9.5.1
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.4.2-canary.0
eslint-config-next: 13.4.1
react: 18.2.0
react-dom: 18.2.0
typescript: 5.0.4
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://github.com/benrandja-akram/next-js-revalidate-with-redirec-bug
To Reproduce
- go to profile page
- submit form
- go to profile page again
- submit form again
- go to profile page
- Cache is used and profile page shows stale data.
Describe the Bug
client side cache is not revalidated When calling revalidatePath with a redirect in server action
Expected Behavior
revalidate client side cache when calling revalidatePath + redirect i nserver action
Which browser are you using? (if relevant)
chrome
How are you deploying your application? (if relevant)
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 20
- Comments: 21 (15 by maintainers)
From what I’ve seen in my repro + others is that it’s not just for 1 page. It only works on the current path. So even a single
revalidatePath
call is not working as intended.Example: Current url
/dashboard/posts/new
revalidatePath called on/dashboard/posts
Next Link navigation to/dashboard/posts
=== stale data.Ran into this bug as well. This is not just with redirect, its with all revalidatePath usage.
Here’s my repro: https://github.com/sannajammeh/app-router-revalidation-not-refreshing
As usual run
pnpm install
npx prisma db push
pnpm dev
/player
in the browserObservations:
testing
into input field and press updateAll Items useRouter() 'forceOptimisticNavigation1'
Results:
Root Layout
- Always updatesChild page
- Always staleChild page Layout
- Always staleChild page with router.push
- Always staleChild page with router.push & forceOptimisticNavigation
- Updates once, subsequent updates always staleServer console
: Actually logs prisma performing select query after revalidation.If you continue adding random numbers while updating, you will soon see each link giving you a different number.
This is very weird caching behavior. Imho, the
Link
&useRouter
hook are too limited in controlling cache on the client side. Ideally ahard
navigation prop should be present on theLink
component andforceOptimisticNavigation
should be documented.I have the same issue, any ideas how to fix this cache issue?
This was fixed by #50848 in the latest canary. Let us know if it’s still not working correctly, thanks!
That is correct, but we are looking for hard navigation using SPA. That way we wont unmount parent layouts.
@vasco3 using <a href will trigger a hard navigation
@arnasofc a temporary workaound would be calling router.refresh in a client component
@eric-burel yeah I want. to revalidate nested layout when I use a server action when submitting a form. The client state isn’t updated with the revalidatePath so even if I navigate (with Link) back and forth of different routes the data is stale. The only way to get fresh data is to navigate with
a
anchor or I assumewindow.location.href
.