oak icon indicating copy to clipboard operation
oak copied to clipboard

Deregister a Route (or Router)

Open shelbycotton opened this issue 4 years ago • 14 comments

This might be a long shot, but is there a way to dynamically remove routes (or routers) in Oak at runtime? It looks like you can do this via router.stack in Koa but in Deno the stack has a private identifier and cannot be accessed.

Thanks so much!

shelbycotton avatar Feb 23 '21 18:02 shelbycotton

Not currently, though I can see the need for something like this.

kitsonk avatar Apr 09 '21 00:04 kitsonk

I'm looking to also add routes at runtime, I would assume this goes hand-in-hand with deregistering routes at runtime. Is that currently available?

geoffreysmith avatar Aug 29 '21 04:08 geoffreysmith

In our application there's a requirement that routes can be re-registered when code is dynamically updated without a reboot. I'd rather not create an entirely new Application everytime this happens.

Therefore +1 for de-registering a route.

garethj2 avatar Feb 01 '23 12:02 garethj2

up for this feature ?

Plawn avatar Jan 29 '24 22:01 Plawn

I took a look at it a while ago. It wasn't as straight forward as I had initially assumed and quickly got to a level of complexity.

One of the challenges is how to make it work. The current router and middleware APIs are fluent, which means returning a handle to a handler isn't straight forward. We could take the approach DOM event handlers are with the likes of .addEventListener() and .removeEventListener() where it only works by strict equality of the handler, but that is always a clunky API.

But even if we did that, the way layers are composed in the middleware stack is quite complex with a fair amount of indirection that was always engineered to make it easy to add route that behave as expected, but complicated to remove routes.

Neither express nor koa-router have APIs for this for some of the same reasons I mention above (as well as the challenge of how to handle inflight requests and ensure that the middleware stack doesn't break, etc.)

kitsonk avatar Jan 30 '24 01:01 kitsonk

Pretty common to have a feature where users can create pages that update the routing table basically. Are we talking about the same thing?

geoffreysmith avatar Jan 30 '24 01:01 geoffreysmith

Adding routes (or middleware) at runtime is not the issue. That is already supported.

Most client side routing that "add" pages are parametrised versions of a route, not new "logic", and even if the logic is incrementally added it isn't usually removed without a complete page reload.

On the surface it seemed like a reasonable request to deregister a route, but it is complicated on several fronts and actually the use case is not clear to me. Under what conditions would you want to deregister a route?

kitsonk avatar Jan 30 '24 03:01 kitsonk

I have a main app that controls the Application instance and a main Router Then it delegates other parts of the app by creating a router and adding it to the main router with a prefix and giving it to the sub application that can then create routes on this sub router.

If I want to unregister the sub app, today it's not possible

Plawn avatar Jan 30 '24 14:01 Plawn

What happens to events in situ if you deregulated a route and a task has not been completed on the deregistered route?

geoffreysmith avatar Jan 30 '24 15:01 geoffreysmith

If the events are lost, it's not an issue, it's up to the subApp and the mainApp to ensure the router is unmounted at a correct moment. It should not rely on an automatic feature of the router

Plawn avatar Jan 30 '24 15:01 Plawn

If I want to unregister the sub app, today it's not possible

What is the use case for unregistering a sub app? Why would the server want to "forget" something? I am trying to understand the use case. The more specific would help in understanding.

kitsonk avatar Jan 30 '24 20:01 kitsonk

I am also confused. In my use for simplicity the router never forgets it, it turns into a 301 redirect, 404 or 500. So I guess the question is if the route goes away does it get an error response or does it go in a black hole? I guess maybe if you had a better use case besides the abstract main app and sub app that would help. I am thinking in terms of code branching, if you take a bad path you want an error not for the app to crash.

geoffreysmith avatar Feb 01 '24 18:02 geoffreysmith

I want to be sure i have a blank canvas, the next time I remount my app, if remounting the same router on top of the last one works, then I might suffice

Plawn avatar Feb 01 '24 20:02 Plawn

I want to be sure i have a blank canvas, the next time I remount my app

What do you mean by "remount" an app. There is no such method named that in oak.

kitsonk avatar Feb 04 '24 08:02 kitsonk