Struggling to revalidate cache on vercel

Hi gang, I’m a bit stuck with how to handle cache revalidation with Nextjs and Vercel.

I have an api endpoint where I get some posts from, and I tagged the request so I can later revalidate the cache:

const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/get-public-posts`, {
        next: {tags: ['public-posts'] } 
    });

In the api route I have set some headers so that vercel picks these up and uses the Edge cache:

 return new Response(JSON.stringify({ ads, total }), {
          status: 200,
          headers: {
            'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=59', 
            'Content-Type': 'application/json',
            'Cache-Tag': 'public-posts',
          },
        });

And Finally, when a new post is added, I’m running:

 revalidateTag('public-posts');

Which doesn’t seem to do anything. It all works fine after 1 minute, when the cache expires on its own, but it doesn’t work when I try to clear the cache manually.

Am I doing this wrong?

I did ISR with Astro on Vercel some time ago and they were using the concept of bypassToken, x-prerender-revalidate header. I’ve not come across Cache-Tag on Vercel docs. I can be wrong but it seems like applying concept from another CDN to Vercel.

Hi @valentinilas, welcome to the Vercel Community!

I see that you are using the fetch next cache as well as the Edge network caching using the headers. The revalidateTag function will purge the fetch cache but not the Edge network cache.

This means that on the initial request the data is cached both in the application and at the Edge network. Calling the revalidateTag function before the second request and 60 seconds will purge the fetch cache but not the Network cache.

I don’t think the Cache-Tag header is used by Vercel.

I hope this clarifies your doubt.

Hi Anshuman, thank you! And that makes sense!

Just wondering if the network cache can also be programatically revalidated, similar to Next’s revalidateTag?

Hi @valentinilas, glad to help you out.

No, Vercel doesn’t yet provide an API to purge the Edge network cache. You have to rely on the HTTP header directives such as max-age.