Next.js › Caching
How do you control how long a fetched piece of data stays cached before it is considered stale?
IntermediateAnswer
You pass a revalidate option to the fetch call, specifying the number of seconds after which Next.js should fetch fresh data again instead of using the cached version, giving you fine grained control over how up to date each piece of data needs to be.
const res = await fetch('https://api.example.com/posts', {
next: { revalidate: 60 }
});Real-world example A blog revalidates its list of posts every sixty seconds, so new articles appear reasonably quickly without needing to rebuild the entire site or fetch fresh data on every single request.