Next.js Discord

Discord Forum

Is there a way to run a callback function on revalidate?

Unanswered
Chippiparai posted this in #help-forum
Open in Discord
ChippiparaiOP
I am using a Cloudfront distribution in front of nextjs 14.1 (vercel not an option at my company). I currently have an on-demand revalidation setup where a CMS webhook calls my revalidate endpoint, which triggers Next revalidate and Cloudfront invalidate for a specific route.

The problem is, there are situations where I'd like to revalidate a cache tag, which can affect multiple routes, and I don't know of a way to pass these routes to Cloudfront for invalidation. I haven't been able to find anything like this in the docs, so this is hypothetical, but there are a couple solutions I could see:

1.) Define an "onRevalidate" callback for a dynamic route that I can use to invalidate Cloudfront. I.e. on ...[slug]/page.tsx I could have something like:
export const onRevalidate = (slug) => { cfInvalidate(slug)}


OR

2.) Get a list of all routes associated with a cache tag. I.e. on .../api/revalidate/route.ts I could have something like:
const revalidatedRoutes = getRoutesByTag("posts");
cfInvalidate(revalidatedRoutes));


Does anything like this exist, or can you think of a different solution to invalidate a cache tag's routes on a CDN?

0 Replies