nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.25k stars 514 forks source link

Ability to use `routeRules` as a function #937

Open VladStepanov opened 1 year ago

VladStepanov commented 1 year ago

Describe the feature

It would be more flexible, if routeRules could call cb on request. I propose the following api:

{
  ...
  routeRules (event) {
    ...
    return {
      headers: {},
      static: true,
      cors: true,
      redirect: {  }
    }
  }

Additional information

pi0 commented 1 year ago

Hi, dear @VladStepanov can you please explain more why you would event base rules?

Route rules are defined at build time and are applied to platform deployment configuration and middleware before even hitting the event handler.

In other cases, you can always apply operations such as wrapping for proxy, adding headers, etc directly from a middleware.

VladStepanov commented 1 year ago

Hi @pi0, thanks for reactive answer!

We have edge case, where nuxt working with subdomains and depending on which subdomain, we should have a different routing.

I will try to describe what is the case. We have site builder like taplink, but sites created by users are placed not in path (taplink.cc/[site_name]) but in subdomain like [site_name].taplink.cc.

For that pages(subdomains) we need a cache. That's why we have to check every request.hostname, take from it subdomain and with that subdomain check cache.

Route rules are defined at build time and are applied to platform deployment configuration and middleware before even hitting the event handler.

Maybe i don't understand something, but there is a possibility to pass event to routeRules https://github.com/unjs/nitro/blob/4a4f539a04eb2b85c81361a62ef8f7ee29773771/src/runtime/route-rules.ts#L18-L22 Correct me if i'm wrong

Maybe there is other way to implement it. Currently i'm researching.

Chippd commented 1 week ago

@VladStepanov did you work out how to do subdomains? I'm currently trying to work out how to:

I have it working when I have server side rendering on, but not when I prerender the routes.