For the route
/worki have a corresponding component where the relevant section is in the first screenshot. The corresponding payload collection for this, has an
afterChangehook called
revalidateTagged.
import type { CollectionAfterChangeHook } from 'payload'
import { revalidateTag } from 'next/cache'
import type { CaseStudy } from '@/payload-types'
export const revalidateTagged: CollectionAfterChangeHook<CaseStudy> = ({
doc,
previousDoc,
req: { payload },
}) => {
if (doc._status === 'published') {
const tag = 'case-studies'
payload.logger.info(`Revalidating tag: ${tag}`)
revalidateTag(tag)
}
return doc
}However, when I make the change to any document on the CMS, and then go back to
/workand refresh, the relevant content wrapped in unstable_cache does not get updated, and the previous cached data is reused. What could the issue be? Please let me know if any more information is required if you haven't fully understood my scenario.
Are you using middleware for auth at all? I ran into a similar issue today where middleware was doing a redirect, even though the revalidation function was saying it succeeded.
no middleware
i made a simple reproduction using the blank template
can someone explain where i've made the goof up
the code of interest is mainly going to be
src/payload.config.ts,
src/app/(frontend)/booksand
src/collections/Bookswhen I add a book
https://media.discordapp.net/attachments/1280125746400067584/1280147085328646164/image.png?ex=66d70561&is=66d5b3e1&hm=ed90a4d7522811b2da400d24f5e691f9e0a3774a9d3bcd63d6f8513fd7e73bc5&=&format=webp&quality=lossless&width=2380&height=816i don't see any issues since clearly
revalidateTaghas been called
and
booksis part of the tag list in the
unstable_cachewrapped fetch to the Books collection
Could you try something in this format:
const getCachedUser = unstable_cache(
async () => getUser(),
['my-app-user'],
{ tags: ['user'] }
);
const getBooks = async () => {
const payload = await getPayloadHMR({ config })
const books = await payload
.find({ collection: 'books', pagination: false })
.then(({ docs }) => docs)
return books
}
const cachedGetBooks = unstable_cache(
async () => getBooks(),
['books'],
{ tags: ['books'] }
)so like this?
Yep
let me try that
bruh why did that work 😭
like actually
why does that work
So the tags object is what’s actually used for revalidation, the second parameter you were trying to use is actually for creating separate caches if needed (e.g. per user id)
what do you mean by separate caches here
Take a look at this example
https://nextjs.org/docs/app/api-reference/functions/unstable_cache#exampleohh so it maintains a cached value per id?
It can do yeah
but what would the purpose of that be?
im a little new to cache revalidation please bear with me 😅
because if we revalidate the tag "books"
then wouldn't all the cached values get refreshed with new values from the data source (e.g payload)
I think an example would be if a user logs out and logs in as someone else in the same session. The user id would change which would invalidate the cache.
the cache containing the value of the signed in user?
are there multiple caches in this scenario you've just described
or is that the only cache
I've updated my repo to be a simple example of cache revalidation in payload cms :)
Star
Discord
online
Get dedicated engineering support directly from the Payload team.