nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Cache invalidation is not working on production (vercel or netlify)

Open cfab opened this issue 9 months ago • 6 comments

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


cfab avatar Feb 18 '25 17:02 cfab

Seems like your example application is hitting the vercel (data) cache (see x-vercel-cache: HIT)?

Image

TheAlexLichter avatar Feb 18 '25 18:02 TheAlexLichter

@TheAlexLichter Yes but we agree that it should not, correct ? I just imported the repo in vervel and deployed it (no settings at all).

cfab avatar Feb 19 '25 10:02 cfab

something similar maybe ? https://vercel.community/t/nuxt-3-nitro-cache-unable-to-invalidate-cache-in-production/5084

cfab avatar Feb 19 '25 12:02 cfab

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);
});

cfab avatar Feb 19 '25 13:02 cfab

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.

drewbaker avatar Feb 21 '25 01:02 drewbaker

I am also still facing the same issue. Is the recommended way to use the workaround with cached function?

vincentdorian avatar Mar 30 '25 10:03 vincentdorian