nuxt-multi-cache icon indicating copy to clipboard operation
nuxt-multi-cache copied to clipboard

Error when using Purge API and custom `enabledForRequest()`

Open bgondy opened this issue 1 year ago • 0 comments

When using a custom enabledForRequest() function, Purge API calls return a 500.

POST http://localhost:3000/-/cache/purge/route
X-Nuxt-Multi-Cache-Token: my-token
Accept: application/json
Content-Type: application/json

["my:key"]
HTTP/1.1 500 Failed to load cache context.

Adding a test case to enabledForRequest() API solves the issue

// multicache.serverOptions.ts
  enabledForRequest(event: H3Event) {
    if (event.path.startsWith('/some-cached-route')) {
      return Promise.resolve(true);
    }

    // …

    // I need to add this to make it work
    if (event.path.startsWith('/-/cache/')) {
      return Promise.resolve(true);
    }

    return Promise.resolve(false);
  },
});

Here is my config:

// nuxt.config.ts
export default defineNuxtConfig({
  multiCache: {
    route: {
      enabled: true,
    },
    api: {
      enabled: true,
      prefix: '/-/cache',
      authorization: 'my-token',
    },
  },
});

IMHO, the purge API should work as-is and should not depend on enabledForRequest(). WDYT?

bgondy avatar May 02 '24 13:05 bgondy