Available since @opennextjs/aws 3.4.0
Not enabled by default. This override is used by OpenNext when routes have been On-Demand revalidated and the CDN needs to be updated. It is not called for ISR revalidation.
It will be called for revalidatePath, revalidateTag and res.revalidate().
If you want to better understand how to implement your own Automatic CDN Invalidation, the easiest way would be to take a look at the existing included Automatic CDN Invalidation (opens in a new tab)
Custom implementations
An Automatic CDN Invalidation override must implement invalidatePaths and can optionally implement invalidateTags:
type CDNInvalidationHandler = {
name: string;
invalidatePaths: (paths: CDNPath[]) => Promise<void>;
invalidateTags?: (tags: string[]) => Promise<void>;
};invalidatePaths
Invalidates the CDN entries of the given paths. Each path includes the path before (initialPath) and after (rawPath) routing as well as the routes that matched while resolving the request (resolvedRoutes).
For revalidateTag and revalidatePath, the paths are resolved from the tags by the Tag Cache. Tag caches in nextMode can only resolve paths when they implement the optional getPathsByTags method.
invalidateTags
Available since @opennextjs/aws 4.1.14
If your CDN supports invalidating its cache by tags, you can implement invalidateTags to invalidate the original cache tags directly instead of resolving them to paths.
- It is called once per
revalidateTagorrevalidatePathcall, after the tag cache has been updated, with the tags as passed to Next.js: hard tags fromrevalidateTag()and soft tags (prefixed with_N_T_/) fromrevalidatePath(). Tags are not expanded, i.e. the hard tags of the paths matching a soft tag are not added. - When it is provided,
invalidatePathsis no longer called forrevalidateTagandrevalidatePath. It is still called forres.revalidate()in the Pages Router. - The CDN integration must associate cached entries with the same tags, both hard and soft, for the invalidation to have an effect.
Included Automatic CDN Invalidation
dummy
The Dummy AutomaticCDNInvalidation is a dummy implementation that will do nothing. It is the default implementation.
cloudfront
Cloudfront invalidation can be very expensive. Manual CloudFront path invalidation incurs costs. According to the AWS CloudFront pricing page (opens in a new tab):
No additional charge for the first 1,000 paths requested for invalidation each month. Thereafter, $0.005 per path requested for invalidation.
This implementation will send an invalidation request for every route that needs to be invalidated (the request invalidates 2 paths, one for the route itself and one for the data route).
Tag cache invalidation could end up triggering thousands of invalidation requests.
Only use this implementation if you are aware of the costs and are willing to pay for it.
The CloudFront Automatic CDN Invalidation will invalidate the cache of the CloudFront distribution.
Requirements
- You need to provide the
CLOUDFRONT_DISTRIBUTION_IDenvironment variable to your server.