Tag-based revalidation inconsistent bahavior
Answered
Mini Lop posted this in #help-forum
Mini LopOP
Has anyone been able to make tag-based revalidation work consistently in the app router?
I'm using sanity and I have my queries tagged with a single tag, which is revalidated whenever I do an update in Sanity (with a webhook). But for some reason, my home page doesn't get the updated, while other pages do.
I'm using sanity and I have my queries tagged with a single tag, which is revalidated whenever I do an update in Sanity (with a webhook). But for some reason, my home page doesn't get the updated, while other pages do.
const res = await client.fetch<T>(
getter(params),
{},
{
next: {
tags: ['sanity'],
},
},
);92 Replies
remember that there is page level cache and fetch cache
so you may need to revalidate the root page also
Mini LopOP
So how are they related? I gets quite confusing with the app router
does Next statically generate the page, and when a request is revalidated it regenerates it?
But I'll try to add revalidatePath too, see if that helps
Mini LopOP
bit conflicted on this line
Calling the revalidatePath method revalidates the Data Cache, which in turn invalidates the Full Route Cache.
does this imply that revalidating the data cache also invalidates the full route cache?
or only when you call revalidatePath?
i haven't used it enough to answer that from knowledge
Mini LopOP
Alright, thanks anyway
does it work with revalidatepath tho?
Mini LopOP
still testing that
@Mini Lop does this imply that revalidating the data cache also invalidates the full route cache?
yeah but it does sound like this is the intended case...
wait idk about the reverse (revalidatepath does the tags, but does it occure other way arround)
Mini LopOP
revalidatePath didn't update it either
only deployment updates it
hmm thats some weird cache then ðŸ˜
Mini LopOP
oh, update
so, the page that's not properly updating has 3 cached requests
they all use the same revalidation tag
and only the first request remains stale
hmm so second is correct new data
Mini LopOP
yes
ohh its doing that revalidate thingy then.... i forgot about it... i noticed it when specifying revalidate time and if it is over the time you get stale untill reload (as it recomputes the new at that time
Mini LopOP
wait, didn't get what you mean
like:
1. page with revalidate = 10
2. acceses page after 10 sec
3. reload page to see stale data
4. reloads again and new data (because it did the new page compute in step 3 but only show for new visit)
2. acceses page after 10 sec
3. reload page to see stale data
4. reloads again and new data (because it did the new page compute in step 3 but only show for new visit)
Mini LopOP
but it stays stale
the data for the first request
literally infinite
until I redeploy
oh
i don't know what other cache is there then...
Mini LopOP
so, the page is not cached, that's clear
so it must be that single request
can you show your code then... maybe you have something else caching it
Mini LopOP
@Mini Lop Click to see attachment
oh your
export const dynamic = sanityDynamic is the prob i thinkthe value has to be static
as in a string
because nextjs doesn't run the code and just looks for it
Mini LopOP
can't use env either?
not that i know of
like you can't even do
export const revalidate = 60 * 12but in dev there is no cache anyway
Mini LopOP
ok, let me try this
but, I actually want it to fallback to the default (which it would in this case, right?)
as in, cache
@riský but in dev there is no cache anyway
*for some reason fetch cache exists actually (even though they make you think there is no cache)
Mini LopOP
or you think it's best to do force-dynamic in prod?
and rely on the data cache
im saying it prob doesn't do anything when using
next devMini LopOP
so the env variable doesn't matter
@Mini Lop or you think it's best to do force-dynamic in prod?
wait that makes it have no cache, i would say its still best having it as it can be faster cached
but both should work well too
Mini LopOP
ideally it would be statically generated
and regenerated when the tag is revalidated
yeah so if you leave it as default and revalidate tag and path, it should work in theory?
Mini LopOP
in theory yes
let me remove the 'dynamic' and see if it interferes in some way
as revalidatepath is meant to override that
Mini LopOP
still strange, because all other pages have the exact same setup
as like in this example:
* no cache specified in home: https://github.com/leerob/on-demand-isr/blob/main/app/page.tsx
* revalidate homepage: https://github.com/leerob/on-demand-isr/blob/main/app/api/webhook/route.ts#L33 (and next req would build latest)
* no cache specified in home: https://github.com/leerob/on-demand-isr/blob/main/app/page.tsx
* revalidate homepage: https://github.com/leerob/on-demand-isr/blob/main/app/api/webhook/route.ts#L33 (and next req would build latest)
Mini LopOP
it's cached in dev when I remove it 😄
maybe tag revalidation and path revalidation don't work well together
nextjs cache has changed too much since i last played with it ðŸ˜
Mini LopOP
so I can confirm that export const dynamic = sanityDynamic; does work
and also has effect in dev
@Mini Lop so I can confirm that export const dynamic = sanityDynamic; does work
it shouldn't do much if working based on what i was toled some time ago
@Mini Lop and also has effect in dev
dev may not do static analysis tho
Mini LopOP
so, funny thing, when I deploy, the data is updated
when I do revalidatePath, the data is stale again
wth
You said when you update something in sanity, so is it triggering a webhook that hits a route handler that calls revalidatePath?
^ because this behavior does not work at all for me
Mini LopOP
yes
can you show me your fetch code, because there's also a mistake in the sanity-next doc
Yeah, the revalidate functions straight up do not work in Route Handlers.
at least in my experience
@Mini Lop can you show me your fetch code, because there's also a mistake in the sanity-next doc
I'm revalidating path since my data is fetched from the CMS client, it's not tagged.
btw we are using latest nextjs right?
yeah
Mini LopOP
I'm using 13.4.4
I can try 14
@Mini Lop I can try 14
yes try 14
revalidation is just plain buggy in 13
Answer
so you might just be hitting bugs
I couldn't have revalidateTag to work on v13 but it worked fine in v14
Mini LopOP
yep, that did the trick
@Eric Burel I couldn't have revalidateTag to work on v13 but it worked fine in v14
how about revalidate path? I can't seem to get that to work from a route handler trying to revalidate a fully static page.
I'm on the latest version