Skip to content
tezvyn:

Next.js Caching: When and How to Revalidate Data

Source: nextjs.orgHardHow cards are made

Next.js aggressively caches data by default. Revalidation is how you tell it the data is stale, either after a set time or on-demand after a mutation. The footgun is forgetting fetch is cached; use revalidatePath or revalidateTag to bust the cache.

Why it exists

Modern web apps need to be both fast and accurate. Serving data from a cache is fast, but the data can become stale. Fetching fresh data for every request is accurate but slow. Caching with revalidation provides a necessary compromise, allowing developers to define rules for when to invalidate and refetch data.

The mental model

Think of the Next.js data cache as a smart pantry. By default, it stocks an item (a piece of data) and keeps it forever. Revalidation is your instruction list for the pantry. Time-based revalidation is like a note saying, "Toss the milk after 7 days." On-demand revalidation is like pressing a button that says, "We just used the last egg, go buy more now."

How it works

Next.js extends the native fetch API to integrate with its caching system. By default, any data fetched with fetch in a Server Component is cached indefinitely. You control this behavior in two main ways. First, for time-based revalidation, you add an option to your fetch call: fetch('...', { next: { revalidate: 3600 } }) to refetch the data at most once per hour. Second, for on-demand revalidation, you use functions like revalidatePath('/products') or revalidateTag('product-list') inside Server Actions. These are typically called right after a database mutation to invalidate the relevant cached data.

When to use it

Use time-based revalidation for data that updates periodically but doesn't need to be real-time, like a news homepage. Use on-demand revalidation for data that changes as a direct result of user actions, such as updating a profile or posting a comment. This ensures the UI reflects the change on the next page load.

When not to use it

Avoid caching for data that must be absolutely real-time and is unique to a user session, like a shopping cart's contents. For these cases, you can explicitly opt out of caching by using fetch('...', { cache: 'no-store' }). Using dynamic functions like cookies() or headers() also opts a route out of static caching.

One canonical example

A user submits a form to update their bio. The form calls a Server Action. This action first updates the bio in the database. Immediately after the database write succeeds, the action calls revalidatePath('/profile'). The next time anyone navigates to /profile, Next.js knows its cached version is stale and will fetch the new data, displaying the updated bio.

Interview question

After a user updates their profile picture, what is the most appropriate way to ensure the new image is displayed?

  • a.Implement revalidatePath('/profile') in a Server Action after the database update.Correct
  • b.Configure the fetch call for the profile data with revalidate: 3600 to update hourly.
  • c.Rely on Next.js to automatically detect the database change and revalidate the cache.
  • d.Disable caching for the profile data using cache: 'no-store' in the fetch call.
Why?

Option A uses on-demand revalidation, which is specifically designed for data that changes as a direct result of user actions, ensuring the UI reflects the change on the next page load. Option B, time-based revalidation, is for data that updates periodically, not for immediate updates after a user action.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nextjs — each one lists the topics its interview covers.

See open roles