The Vercel Data Cache is a specialized, granular cache for storing responses from fetches while using modern 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.
Vercel Data Cache is a shared cache, meaning users of the same project share the same cache.
- 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 13 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 Next.js 13 with App Router:
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 '...';
}
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 '...';
}
import { Request, NextResponse } from 'next/server';
import { revalidateTag } from 'next/cache';
export async function POST(req: Request) {
revalidateTag('blog'); // Purge all data with the 'blog' tag
return NextResponse.json({ revalidated: true, now: Date.now() });
}
For information, see the Limits and pricing doc.
Was this helpful?