nanostores / router

A tiny (673 bytes) router for Nano Stores state manager
MIT License
246 stars 19 forks source link

Regexp routes redirecting #31

Open ivan-suhorukov opened 8 months ago

ivan-suhorukov commented 8 months ago

Now, when i use a RegExp routes, i cannot use openPage or redirectPage functions. Router throws RegExp routes are not supported. So may be we can support this by adding second converter to route? Something like this:

export const $router = createRouter({
  profile: [/\/(student|teacher|manager)\/profile/, (urlRole) => { return { role: urlRole }}, (urlRole) => `/${urlRole}/profile`],
                                                                                 where it is  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
})

So router can determine the redirect url and do redirect?

ai commented 8 months ago

Is it better just pass URL to router.open(url)?

Defining function looks like pretty complex thing.

ivan-suhorukov commented 8 months ago

router.open(url) uses hard-coded url`s, so maybe better to user $router as a one source of truth?

When i change the route then, i dont need to change all of the router.open(url) calls.

ai commented 8 months ago

When i change the route then, i dont need to change all of the router.open(url) calls.

Do you really often change URLs?

We can add this feature, let’s see how many bytes it takes and how it will affect complexity of types and logic.

schwiet commented 2 months ago

What is the recommended workaround? It seems that I can't use regexp routes if I want to do any redirects to that route. In my case the route with a regexp is my default route, so I redirect any invalid route to it.

ai commented 2 months ago

@schwiet just use router.open() with manually written URL.

If you want to change API you need to help and send PR.