Invalidate a static page's cache #21903
-
|
From my understanding, Next.js currently supports incremental static generation/regeneration and allows you to invalidate the cached page after a given amount of time. This works great for pages with data that never changes, or pages with data that change frequently. However, I've had several use cases where data rarely changes, but when it does, the update needs to happen quickly. For instance, with eCommerce, products don't necessarily change that often. But when something like the price changes, the page should update accordingly. So if you do: export async function getStaticProps(context) {
return {
props: { data },
revalidate: 300, // 5 minutes
}
}It provides near real-time updates but results in many useless database reads and regenerations. Especially for pages that may not be updated for weeks or months. And if users don't view this specific page every X minutes, all users effectively see uncached pages. But if you use a longer time interval: export async function getStaticProps(context) {
return {
props: { data },
revalidate: 604800, // 1 week
}
}You get all the user experience benefits you'd need except that crucial data may be out of date for up to a week. That's a bad user experience if they add a $39 item to their cart only to find out at checkout that it's really $49! Is there any way to achieve this behavior? Ideally like being able to create an API route / webhook to invalidate the cache by URL. Then when the data source updates, it can send that webhook to tell Next.js to purge the cache for that page. Relevant discussion: #10721 (comment) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
Cross linking this to #16488 which is a request for the same: regeneration based on an event, to cut short the wait for a revalidation period. |
Beta Was this translation helpful? Give feedback.
-
|
You can do that even now by hacking through it. Just delete under .next/pages the page you want to invalidate, easy as that ;) |
Beta Was this translation helpful? Give feedback.
-
|
I'm incredibly excited to share that Next.js 12.1 was just released, which includes on-demand Incremental Static Regeneration! We know many of you have been looking forward to this for a while, including myself, and I'm so happy it is here. |
Beta Was this translation helpful? Give feedback.
I'm incredibly excited to share that Next.js 12.1 was just released, which includes on-demand Incremental Static Regeneration! We know many of you have been looking forward to this for a while, including myself, and I'm so happy it is here.