Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing nitro cache from handlers doesn't work as intended #2738

Open
dencs08 opened this issue Sep 14, 2024 · 0 comments
Open

Clearing nitro cache from handlers doesn't work as intended #2738

dencs08 opened this issue Sep 14, 2024 · 0 comments

Comments

@dencs08
Copy link

dencs08 commented Sep 14, 2024

Environment

  • Operating System: MacOS Darwin
  • Node Version: v22.7.0
  • Nuxt Version: 3.13.0
  • CLI Version: 3.13.0
  • Nitro Version: 2.9.7
  • Package Manager: [email protected]

Reproduction

  1. create api endpoint with defineCachedEventHandler
  2. set basic parameters like maxAge, name, getKey (optional for debugging)
  3. remove the cache files manually or by running:
  const storage = useStorage("cache");
  try {
    // Fetch keys for both nitro:handlers and nitro:functions
    const handlerKeys = await storage.getKeys("nitro:handlers");
    const functionKeys = await storage.getKeys("nitro:functions");

    // Combine both sets of keys
    const cacheKeys = [...handlerKeys, ...functionKeys];

    // Remove all cache items concurrently
    await Promise.all(cacheKeys.map((element) => storage.removeItem(element)));

    return { success: true };
  } catch (error: any) {
    console.error("Error invalidating cache:", error);
    throw createError({
      statusCode: error.statusCode || 500,
      statusMessage: error.message || "Failed to invalidate cache",
    });
  }
  1. observe that files get removed from disk
  2. Api endpoint is still returning the cached response (network tab size - "(disk cache)") - the response was cached on the client browser

Describe the bug

When creating api endpoint with defineCachedEventHandler or cachedEventHandler even after clearing the cache in nitro it still returns cached responses due to client browser caching the functions.
Using:

    shouldBypassCache,
    shouldInvalidateCache

runs them only when the response isn't cached in the browser making it impossible to bypass or invalidate it in the endpoint itself without clearing browser cache beforehand.

Additional context

Current workarounds I've found:

Adding belows code to the definedefineCachedEventHandler ensures that clients browser won't cache the function which ensures we now have full control over the cache and can invalidate it manually. We can even use the shouldBypassCache or shouldInvalidateCache. and bypass the cache on will which wasn't previously possible due to browser cache.

    event.node.res.setHeader(
      "Cache-Control",
      "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"
    );
    event.node.res.setHeader("Pragma", "no-cache");
    event.node.res.setHeader("Expires", "0");
    event.node.res.setHeader("Surrogate-Control", "no-store");

    const uniqueId = Date.now().toString();
    event.node.res.setHeader("X-Response-ID", uniqueId);

Second workaround:

Creating a defineCachedFunction and then using it in defineEventHandler makes it possible to clear cache manually. This approach ensures that the client browser doesn't cache the response so we don't need to create unique headers for each request.

Note:
If this is intended behaviour please do let me know. It seems odd that it isn't mentioned in the nitro documentation (or I just haven't look for it good enough). Regardless if it is in the docs I think it might be good to mention it here: nitro cache docs.

Logs

No response

@dencs08 dencs08 changed the title Clearing cache from nitro:handlers doesn't work Clearing nitro cache from handlers doesn't work as intended Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant