revalidatePath invalidates all route cache
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Is this a known bug? I see no one mention it yet but calling revalidatePath in a server actions invalidates all route cache not just the one specified.
11 Replies
Interesting, I didn’t test about it but I guess it’s a bug or something
Bridled Titmouse
I think you can't specify a specific "url" like "/blog/post-1", you have to revalidatePath /blog/[[...slug]] , which will invalidate all urls under blog/
Yes I have checked the docs, https://nextjs.org/docs/app/api-reference/functions/revalidatePath#parameters
Seems like it can't be a specific page if it's a dynamic route.
Seems like it can't be a specific page if it's a dynamic route.
Sorry to drudge this up, but I'm having the same issue with the latest NextJS 13 app. I'm calling
revalidatePath("/foo", "page") within a route action and then when I navigate to /bar (after previously going there before running the revalidatePath() function), it then regenerates the page on the server and passes it down via XHR. It seems like it's invalidating all cache.@Spectacled bear Is this a known bug? I see no one mention it yet but calling revalidatePath in a server actions invalidates all route cache not just the one specified.
Lapland Longspur
Are you doing revalidate(/). Because that will revalidate everything I think.
You can revalidate a specific path for example revalidate(/oneOfYourPage).
But you have to be careful with this. Because if the page you revalidating has other sever actions that you don’t want to revalidate will end up getting revalidated too.
Alternatively you can use revalidateTag(“tagNameâ€). That will revalidate a specific server function that you have tagged, without revalidating anything else in the page.
Let me know if that helpsðŸ‘ðŸ½
https://nextjs.org/docs/app/api-reference/functions/revalidateTag
You can revalidate a specific path for example revalidate(/oneOfYourPage).
But you have to be careful with this. Because if the page you revalidating has other sever actions that you don’t want to revalidate will end up getting revalidated too.
Alternatively you can use revalidateTag(“tagNameâ€). That will revalidate a specific server function that you have tagged, without revalidating anything else in the page.
Let me know if that helpsðŸ‘ðŸ½
https://nextjs.org/docs/app/api-reference/functions/revalidateTag
@meenie Sorry to drudge this up, but I'm having the same issue with the latest NextJS 13 app. I'm calling `revalidatePath("/foo", "page")` within a route action and then when I navigate to `/bar` (after previously going there before running the `revalidatePath()` function), it then regenerates the page on the server and passes it down via XHR. It seems like it's invalidating all cache.
Spectacled bearOP
Newest is NextJS 14, so maybe try updating. If it's not fixed yet you can try something like this: https://github.com/Apestein/nextflix/blob/main/src/components/link-button.tsx
Replace the Next.js's Link component with this component and it will revalidate that route on every visit. It's kinda hacky but it works.
Replace the Next.js's Link component with this component and it will revalidate that route on every visit. It's kinda hacky but it works.
Shoot, meant NextJS 14. And I’m not doing
relvalidatePath("/"), I'm doing revalidatePath("/foo"). I’ll look into the shared server actions thing.@Spectacled bear Newest is NextJS 14, so maybe try updating. If it's not fixed yet you can try something like this: https://github.com/Apestein/nextflix/blob/main/src/components/link-button.tsx
Replace the Next.js's Link component with this component and it will revalidate that route on every visit. It's kinda hacky but it works.
I'd rather not have a hacky work around like that, but I appreciate you providing a possible solution!.
@meenie I'd rather not have a hacky work around like that, but I appreciate you providing a possible solution!.
Spectacled bearOP
Only way to make NextJS 13/14 works is with "hacks"🤣
Haha, ya, no framework can work 100% in all scenarios :).
80% of the time it works all the time.