[Website Template] Revalidate the Archive Block data automatically #15722
Replies: 10 comments
-
|
Can confirm the issue. I could modify what would be an official solution to achieve revalidation of pages that use certain collections? |
Beta Was this translation helpful? Give feedback.
-
|
Same problem. Archive Block does not re-evaluate collections when deploying a project. |
Beta Was this translation helpful? Give feedback.
-
|
@fancier21 @schlesingermatthias revalidateTag('latest_posts')async function getLatestPosts() {
const payload = await getPayload({ config: configPromise })
const posts = await payload.find({
collection: 'posts',
draft: false,
limit: 3,
....
})
return posts.docs
}
export const getCachedLatestPosts = () =>
unstable_cache(async () => getLatestPosts(), [], {
tags: [`latest_posts`],
}) |
Beta Was this translation helpful? Give feedback.
-
|
concreo It works, but only after manually reloading the page. I wonder if it is possible to refresh the data, for example, when switching from one page to another? |
Beta Was this translation helpful? Give feedback.
-
|
@ed-dev-pro, This happens because client-side navigation with Next Link is reusing a cached (or prefetched) version of your page that wasn’t refreshed after the cache tag was invalidated. When you trigger revalidateTag('latest_posts'), the server cache is cleared so that on a hard refresh the server fetches fresh data. However, during client-side transitions Next.js use a version that was prefetched earlier and doesn’t automatically refetch the new data. Refer to this doc To address your issue, you can take a look at the next/link prefetch option or play with the experimental staleTimes feature. Alternatively, you can fetch the latest posts only on the client, in a useEffect, for instance. Note that your question is a Next.js concern, not a payload one. |
Beta Was this translation helpful? Give feedback.
-
|
Got it, thank you! |
Beta Was this translation helpful? Give feedback.
-
|
In addition to this issue, I've noticed the website template is also missing revalidation for the Meta > Related Posts field. Publishing or unpublishing a related post does not invalidate the cache for the parent post that references it. Furthermore, the problem is particularly acute for Media collection items. Since they are used in countless places (like rich text fields, hero images, and SEO meta tags), ensuring proper cache invalidation for them is especially challenging. These specific examples highlight a broader challenge: the template requires a deep understanding of cache invalidation patterns. To improve the developer experience, especially for those new to Payload, would it be possible to:
I believe this would significantly reduce the learning curve and prevent frustrating caching issues. |
Beta Was this translation helpful? Give feedback.
-
|
@concreo @fancier21 @schlesingermatthias I've been looking into the caching behavior and noticed that the To mitigate issues like #10750, I propose a workaround by adding the
While this is not a perfect long-term solution (as it applies a one-size-fits-all revalidation period instead of on-demand updates), it has the significant advantage of making the revalidation behavior consistent across all major pages of the site, which improves predictability and user experience. If I don't hear any objections by EOW, I will proceed with creating the PR. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the detailed discussion and proposed approaches. The issue with pages not automatically revalidating when posts are updated is a caching / Next.js behavior challenge. Workarounds like revalidateTag or client-side fetching help, but there’s no perfect out-of-the-box solution. Overall, this is more of an enhancement to the template than a bug, so I am going to convert it to a feature request. We would be happy to review and accept a PR to improve the template in this area |
Beta Was this translation helpful? Give feedback.
-
|
I opened draft PR #17405 using an Archive query cache tag plus publish/unpublish/delete invalidation. The PR explicitly documents the one-line access-control overlap with #15944 and will be rebased if that lands first. Focused tests pass; the remaining template CI matrix is still running. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the Bug
To prevent caching issues, it's crucial to note that updating or creating a new post does not automatically refresh pages displaying the Archive Block. This could lead to outdated information being presented to users.
Link to the code that reproduces this issue
https://github.com/payloadcms/payload/tree/main/templates/website
Reproduction Steps
Use the basic website template.
Which area(s) are affected? (Select all that apply)
area: templates
Environment Info
Beta Was this translation helpful? Give feedback.
All reactions