Next.js Discord

Discord Forum

multiple fetches + revalidateTag doesn't revalidate every page

Unanswered
Serengeti posted this in #help-forum
Open in Discord
SerengetiOP
I have a fetch that is called from multiple pages. When I revalidate this tag it does not revalidate every page that this fetch is called from.

Some of the fetches occur in ccomponents (server) and some happen on the root page. Is there any reason this is inconsistent. I would expect when I revalidate the tag every fetch that is called would be revalidated.

18 Replies

Even if you use revalidate and THEN navigate to it, it wont recompute the changes.
You need to use refresh the page
Iirc the other way was to use server actions
@Clown Even if you use revalidate and THEN navigate to it, it wont recompute the changes.
thats only if the client has cached it already, but yeah i agree
SerengetiOP
@riský basically lets say i have two pages. the /contact page has a fetch in the root page. the /about page has a fetch in a component (they are both the exact same fetch so have the same tag).

When I revalidate this tag, sometimes when i navigate to /contact it redoes the fetches so has the fresh data, and then if i go to /about it uses the old cache data (even after refreshing)

this is just an example it doesnt always work this way, sometimes its the other way round and sometimes they both get fresh data
SerengetiOP
Still having this problem. Happy to provide any further information needed.
SerengetiOP
I think its something to do with dynamic pages and generateStaticParams. If i comment out my generateStaticParams then it all works fine, looks like maybe the router cache isn't being invalidated along with the data cache for all the pages?
probably because as the docs say:
["statically generate routes at build time instead of on-demand at request time"](https://nextjs.org/docs/app/api-reference/functions/generate-static-params)
SerengetiOP
@riský i would still expect the caching to work with it, so if a page that has been generated at build time has a tag that gets revalidated it should get rebuilt when it is requested after invalidation. in fact it does seem to work that way most of the time, but for some reason every now and then the full route cache doesnt get cleared when the data cache is invalidated
if it is static at built time, nextjs could make it invalidateable (as it isn't expected to change... (im not saying that it is what i would want it to do tho)
but then you should just use your solution and not have the function
SerengetiOP
I am doing that right now but it would be good to be able to use the full router cache with static generation and use invalidation. In fact nextjs is supposed to work that way.

https://nextjs.org/docs/app/building-your-application/caching#invalidation

There are two ways you can invalidate the Full Route Cache:

Revalidating Data: Revalidating the Data Cache, will in turn invalidate the Router Cache by re-rendering components on the server and caching the new render output.

As I say, it does seem to work but just not for every page if multiple pages share the same fetch.



I am trying to create a very simple repo which shows this in effect.
i wish i could have a static page but dynamic layout... i don't get that either 😭
SerengetiOP
Okay I think i found something interesting. if I use generateStaticParams this is what happens.

Build project
Pages get generated and cache (/a, /b,) (pages are the same)
Visit /a <-- see cached data
Revalidate cache
Visit /a <-- see new data [this is correct data has been revalidated]
Visit /b <-- see old data from build [this is incorrect data has not been revalidated]

So it seems that if you revalidate a tag of a page that has been generated it will not actually revalidate it if its not been viewed.

If I build the site and visit every page manually, then revalidate the tag then all pages get revalidated fine.
(it also works for paths a,b,c,d,e)
https://github.com/thisispaul/revalidate-test

or fork and deploy yourself. as once pages have been visited and invalidated then the bug is no longer there