Conceptual
4 min read

Vercel Data Cache

Vercel Data Cache is a specialized cache that stores responses from data fetches. Learn more about how it works with Next.js
Table of Contents

Data Cache is available in Beta on all plans

The Vercel Data Cache is a specialized, granular cache for storing responses from fetches while using frontend frameworks like Next.js.

Frameworks that integrate with the Data Cache (currently Next.js) are able to cache data per fetch instead of per route. This means you can have static, dynamic, and revalidated data together in the same route.

With Vercel, you write application code, like component-level data fetching with fetch, and we scaffold globally distributed infrastructure for you with no additional effort. See our examples to learn how to implement this.

  • Ephemeral, globally available, regional cache: Every region in which your serverless or edge function runs has an independent cache, so any data used in server side rendering or Next.js route handlers is cached close to where the function executes.
  • Time-based revalidation: All cached data can define a revalidation interval, after which the data will be marked as stale, triggering a re-fetch from origin.
  • On-demand revalidation: Any data can be triggered for revalidation on-demand, regardless of the revalidation interval. The revalidation propagates to all regions within 300ms.
  • Tag based revalidation: Next.js allows associating tags with data, which can be used to revalidate all data with the same tag at once with revalidateTag. For example, you could use this to revalidate all responses from a CMS with the same author ID tag.

Next.js combines Vercel Data Cache with Incremental Static Regeneration (ISR) to provide optimized caching infrastructure for your pages.

When a page contains entirely static data, Vercel will use ISR to generate the whole page. However, when a page contains a mix of static and dynamic data, the dynamic data needs to be re-fetched when rendering the page. In this scenario, Vercel Data Cache is used to cache the static data to avoid slow origin fetches.

Both ISR and Vercel Data Cache support time-based revalidation, on-demand revalidation, and tag based revalidation.

Vercel's Edge Cache is used for caching entire static assets at the edge, such as images, fonts, and JavaScript bundles. The Vercel Data Cache is used for caching data fetched during a function's execution, such as API responses.

When you deploy a Next.js project that uses App Router to Vercel, the Vercel Data Cache is automatically enabled to cache segment-level data alongside ISR in the app router.

To learn more about managing your Data Cache, including disabling it, and purging the cache, see Managing Data Cache.

These examples use the Next.js App Router:

app/page.tsx
export default async function Page() {
  // Since this page is dynamic, it will run through a Vercel Function
  const dynamic = await fetch('https://api.vercel.app/products', {
    cache: 'no-store',
  });
  const products = await dynamic.json();
 
  // Cache the static data and avoid slow origin fetches
  const static = await fetch('https://api.vercel.app/blog', {
    next: {
      revalidate: 3600, // 1 hour
    },
  });
  const blog = await static.json();
 
  return '...';
}
app/page.tsx
export default async function Page() {
  // Since this page is dynamic, it will run through a Vercel Function
  const dynamic = await fetch('https://api.vercel.app/products', {
    cache: 'no-store',
  });
  const products = await dynamic.json();
 
  // Cache the static data and avoid slow origin fetches
  const static = await fetch('https://api.vercel.app/blog', {
    next: {
      tags: ['blog'], // Invalidate with revalidateTag('blog') on-demand
    },
  });
  const blog = await static.json();
 
  return '...';
}
app/actions.ts
'use server';
 
import { revalidateTag } from 'next/cache';
 
export default async function action() {
  revalidateTag('blog');
}

The Vercel Data Cache is unique per Vercel project, isolated per deployment environment (production or preview).

Cached data is persisted across deployments, unless invalidated through revalidate or programmatically calling revalidateTag or revalidatePath. It is not updated at build time. When invalidated, it will be updated at run time with the next new request to the path that was invalidated.

When a revalidation is triggered, the path or cache tag is marked stale globally in every Vercel Edge Network region. The next request to that path or tag, in any region, will trigger a revalidation and update the cache globally. The regional cache in all regions is purged and updated within 300ms.

For information, see the usage and pricing page for the Data Cache.

Last updated on April 19, 2024