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

Allow passing getKey method to route rules #691

Closed
1 task done
MiniDigger opened this issue Nov 24, 2022 · 0 comments · Fixed by #744
Closed
1 task done

Allow passing getKey method to route rules #691

MiniDigger opened this issue Nov 24, 2022 · 0 comments · Fixed by #744

Comments

@MiniDigger
Copy link
Contributor

Describe the feature

I want to use a custom cache key for my route cache (coming from nuxt but open to implementing a nitro plugin).

Naive me thought I could just pass it like this in my nitro (well, nuxt) config:

export default defineNitroConfig({
  routeRules: {
    '/**': {
      cache: {
        swr: true,
        maxAge: 600,
        name: 'pagecache',
        getKey: (e: any) => {
          console.log('hello', e);
          return 'dum';
        },
      },
    },
  },
});

that doesn't work because nitro can't serialize my method. fair enough.

my next try is a lot more ugly, but I basically did this as a nitro plugin:

export default defineNitroPlugin((nitroApp) => {
  let h = handlers.find(h => h.route === "/**");
  let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler;
  console.log("installing handler", h.route);

  nitroApp.router.use(h.route, cachedEventHandler(handler, {
        group: "nitro/routes",
        swr: true,
        maxAge: 600,
        name: "pagecache",
        getKey: (e: any) => {
          console.log('hello', e);
          return 'dum';
        },
      }
  ), h.method);
});

it doesn't use any api and depends on implementation detail (handlers and cachedEventHandler aren't in scope in my plugin but are in scope in the compiled node-server.mjs), but it would be acceptable to me.

sadly not even that works, because the getKey method in my options object is overriden here: https://github.com/unjs/nitro/blob/main/src/runtime/cache.ts#L120

I am close to commenting that out in the compiled code via sed, but I figured I would open a feature request here first :D

I can't think of a nice api to make my plugin code nicer right now, since I don't have the handler cause it gets created by nuxt, but I would already be happy if cache.ts could be adjusted to not override getKey methods from the outside.

open to other ideas tho, I am willing to help brainstorming (I am MiniDigger#3086 on discord if that helps) and even implement this (haven't touched nitro code before but seems straight forward), my company allows me to work/contribute on this problem on work time.

Additional information

  • Would you be willing to help implement this feature?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant