How to revalidate the cache on api route (on vercel)
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Hi gang, been struggling with this issue for days!
In my app I'm making requests to:
const res = await fetch(
next: {tags: ['public-posts'] }
});
I added some headers in the api route so that vercel picks up the requests and uses the Edge cache, which seems to work:
return new Response(JSON.stringify({ ads, total }), {
status: 200,
headers: {
'Cache-Control': 'public, s-maxage=10800, stale-while-revalidate=59', // 1 day cache
'Content-Type': 'application/json',
},
});
Now I can see that the requests hit the cache, in the vercel interface.
Now, my issue is that I need to revalidate the cache whenever a new post is added.
In a server component I'm running:
revalidateTag('public-posts');
But this doesn't seem to work. Any insights would be much appreciated!
In my app I'm making requests to:
const res = await fetch(
${process.env.NEXT_PUBLIC_BASE_URL}/api/get-public-posts?category=${category}&subcategory=${subcategory}®ion=${region}&search=${search}&page=${page}&pageSize=${pageSize}
, {next: {tags: ['public-posts'] }
});
I added some headers in the api route so that vercel picks up the requests and uses the Edge cache, which seems to work:
return new Response(JSON.stringify({ ads, total }), {
status: 200,
headers: {
'Cache-Control': 'public, s-maxage=10800, stale-while-revalidate=59', // 1 day cache
'Content-Type': 'application/json',
},
});
Now I can see that the requests hit the cache, in the vercel interface.
Now, my issue is that I need to revalidate the cache whenever a new post is added.
In a server component I'm running:
revalidateTag('public-posts');
But this doesn't seem to work. Any insights would be much appreciated!
3 Replies
Boerboel
So when you refresh your page it still doesn't change once you revalidateTag ?
Transvaal lionOP
exactly! I figured it must have somethijng to do with those headers but if I remove them, then I can see in Vercel that it no longer hits the Edge cache
without the headers: