Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

revalidateTag doesn't seem to be working

default discord avatar
lzyaclast year
14

For the route

/work

i have a corresponding component where the relevant section is in the first screenshot. The corresponding payload collection for this, has an

afterChange

hook 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

/work

and 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.

  • default discord avatar
    rilromlast year

    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.

  • default discord avatar
    lzyaclast year

    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)/books

    and

    src/collections/Books

    when 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=816

    i don't see any issues since clearly

    revalidateTag

    has been called



    and

    books

    is part of the tag list in the

    unstable_cache

    wrapped fetch to the Books collection

  • default discord avatar
    rilromlast year

    Could you try something in this format:



    const getCachedUser = unstable_cache(


    async () => getUser(),


    ['my-app-user'],


    { tags: ['user'] }


    );

  • default discord avatar
    lzyaclast year
    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?

  • default discord avatar
    rilromlast year

    Yep

  • default discord avatar
    lzyaclast year

    let me try that



    bruh why did that work 😭



    like actually



    why does that work

  • default discord avatar
    rilromlast year

    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)

  • default discord avatar
    lzyaclast year

    what do you mean by separate caches here

  • default discord avatar
    lzyaclast year

    ohh so it maintains a cached value per id?

  • default discord avatar
    rilromlast year

    It can do yeah

  • default discord avatar
    lzyaclast year

    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)

  • default discord avatar
    rilromlast year

    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.

  • default discord avatar
    lzyaclast year

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.