This guide shows you how content served from an external origin through Vercel’s CDN can be cached and selectively invalidated so it is only regenerated when data changes.
Vercel's CDN improves performance and security when proxying requests to external origins, or origins that are hosted somewhere other than Vercel. The CDN reduces latency by caching content close to the user in all Vercel Regions worldwide, while the Vercel Firewall protects against threats like DDoS attacks, bot traffic, and malicious requests before they reach your servers.
Users want to see content delivered very quickly from anywhere in the world. Caching improves performance by storing and serving the content close to users, but the content also needs to be fresh and updated as soon as data changes. When data changes, you have two options for how to make sure the content is fresh:
- Purge all cached content - This option is too coarse and hurts performance and costs for unchanged content. Users have to make a request back to the origin on the next request which will be slow.
- Purge specific content - This option requires assigning tags to specific pages that are updated at the same time. Content can be invalidated so that a stale page is served to the user on the next request while the cache is refreshed in the background.
Cache tags solve this by letting you organize cached responses into groups that you can invalidate at the same time. Other requests that are tagged differently are unaffected. Cache tags may also be referred to as surrogate keys on other platforms.
The rest of this guide will walk you through how to use and invalidate cache tags in your application.
- A Vercel project with rewrites to an external origin
- An external origin that can set response headers
- The Vercel CLI installed
A rewrite routes a request to a different destination without changing the URL in the browser. Unlike redirects, the user won't see the URL change.
To set up a rewrite, configure the vercel.json with the following code:
The flow for a request will be:
- User requests
yourdomain.com/api/products/123 - Vercel CDN checks for cached content
- If not cached, Vercel fetches from
api.yourdomain.com/api/products/123 - Your server responds with content and cache tags
- Vercel caches the response with those tags
Configure your server to set the Vercel-Cache-Tag header:
What each tag does:
product-123- Invalidate when this specific product changescategory-electronics- Invalidate when any product in this category changesproducts- Invalidate when the product catalog changes
If you have multiple teams, make sure that you have selected the right team:
Now that the Vercel CDN is caching content, you can selectively invalidate specific cache tags to see new content being picked up. When cache tags are invalidated, the next request will return the stale content while the Vercel CDN is refreshing the cache contents in the background. This maintains performance while making sure that subsequent requests get the fresh content.
There are different ways that content can be invalidated on Vercel
Cache tags can be invalidated directly in code if you have a function or webhook that runs when data changes.
Option 1: Direct invalidation in your update function
The invalidateByTags function can be called from server side code in the same project that the tag is defined in.
Option 2: CMS webhook handler
If data is updated in an external CMS, you can create a webhook route handler to receive the event when data changes and choose which tags to invalidate.
The Vercel CLI can also be used to invalidate at any time.
Purge all cache:
Invalidate is the recommended option, but the Vercel CLI can also purge all cache content. This option isn’t recommended since it causes a blocking request to the external origin, but it can be used when all content must be updated.
If you are not able to set cache tags in the response from your external origin, you can still set cache tags on Vercel using routing rules.
Setting cache tags in vercel.json
Setting cache tags from the CLI
Routing rules to set cache tags can also be set using the CLI to add routing rules without requiring you to change any code. Use the vercel routes CLI command:
Cache tags can be incrementally added to your application and optimized based on usage patterns and how often data is updated. Learn more about caching in the Vercel CDN Cache documentation.