Next.js Discord

Discord Forum

Does revalidatePath clear any underlying fetch caches?

Answered
Ratonero Mallorquin posted this in #help-forum
Open in Discord
Ratonero MallorquinOP
As the question says, when calling revalidatePath on a path/url, does NextJS then also empty any fetch call caches that are used as a result of building that path? Or is that still its "own" thing, following any rules for that?

Given the existance of fetch cache tags, it's easy to intertwine these concepts.
Answered by Ray
all the fetching inside that path should be invalidated
View full answer

12 Replies

Answer
@Ray all the fetching inside that path should be invalidated
Ratonero MallorquinOP
Our use case is to for example get 2 things:
1. page specific data via fetch from CMS
2. global less changing data (main menu/footer etc) via fetch from CMS

Is there any way to revalidate only the page specific data, to not needlessly clear global data? Such data would be better cleared with a tag for example, so we don't always want to clear that.
Ratonero MallorquinOP
@Ray But how would we prevent the global data from being invalidated when only invalidating the page specific data?

As you said, all the fetching inside that path should be invalidated, which I assume means the global data will also be invalidated?
like revalidatePath("/users/1") only invalidate the data for /users/1
unless you do this
@Ray you have to specific a path to revalidatePath right?
Ratonero MallorquinOP
If the code to generate that user page on path /users/1 is something like this:

const userData = fetch('https://api:8080/userdata?id=1');
const globalData = fetch('https://api:8080/globaldata');

// ... render page with both user specific data and global data


I assume that a call to revalidatePath("/users/1") will also clear globalData? Because both fetch calls are used to render this page.

Not 100% sure I understood you correctly in the first reply, but I interpret it as such?

Now, I know how to clear globalData specifically (just use revalidateTag), but how can I prevent it from being cleared in this scenario?

Imagine you have 100 users that all use the same "globalData". You don't want to clear this data every time a specific /user/x has to revalidate, only when globalData is updated.
Ratonero MallorquinOP
In other words, I call revalidatePath("/users/1") in this example, it will also clear the data fetch cache for fetch('https://api:8080/userdata?id=1') and fetch('https://api:8080/globaldata')

And there is not way to prevent that, other than making sure they are not used when rendering the users/1 page.

Sounds about right?
Ratonero MallorquinOP
I'll look into that approach instead, thank you!