nitro
nitro copied to clipboard
Cache invalidation is not working on production (vercel or netlify)
Environment
nitro 2.10.4 node: 20.15.1
Reproduction
https://github.com/cfab/github-test-cache-nitro
Describe the bug
It works perfectly when in local development if you go to /api/time you get the time. The returned time is cached for a duration of 60 seconds. if you go to /api/invalidate you precisely invalidate the cache of the other route.
When deployed on netlify or vercel, the cache invalidation is not working. You can check this on https://github-test-cache-nitro.vercel.app/api/invalidate Is this a bug or is this something wrong with my code ?
Same issue opened on nuxt repo: https://github.com/nuxt/nuxt/issues/31030
Additional context
No response
Logs
Seems like your example application is hitting the vercel (data) cache (see x-vercel-cache: HIT)?
@TheAlexLichter Yes but we agree that it should not, correct ? I just imported the repo in vervel and deployed it (no settings at all).
something similar maybe ? https://vercel.community/t/nuxt-3-nitro-cache-unable-to-invalidate-cache-in-production/5084
Successful work-around is to use a defineCachedFunction within a defineEventHandler
-> https://github.com/cfab/github-test-cache-nitro/blob/main/server/api/time2.ts
let time = defineCachedFunction(
async (event) => {
const time = new Date();
return time.toLocaleString();
},
{
maxAge: 40, // 30 seconds
group: "time",
name: "time",
getKey: () => "time",
shouldBypassCache: (event) => {
const { preview } = getQuery(event);
return preview === "true";
},
}
);
export default defineEventHandler(async (event) => {
return time(event);
});
Not sure if it's related, but I've been seeing my API return a 504 error, and that being cached on Vercel today. Only way to get it to retry is to trigger a deploy to wipe the cache, then reload that endpoint and it works again.
I am also still facing the same issue. Is the recommended way to use the workaround with cached function?