Flow: Router: support multiple routes for the same component

Created on 20 Oct 2017  路  7Comments  路  Source: vaadin/flow

When defining a routes map of an application, as a developer, I want to define multiple routes for some views (e.g. both / and /storefront should be handled by the same view).

With the _new_ Router API that is not supported at the moment (flow-1.0.0.alpha6).

A workaround exists:

@Route("")
public class DefaultView extends Div implements BeforeNavigationListener {

    @Override
    public void beforeNavigation(BeforeNavigationEvent event) {
        event.rerouteTo(StorefrontView.class);
    }
}
routing

Most helpful comment

There could be an additional parameter @RouteAlias(value="/some/path", redirect=true) that will make Flow replace url in the browser to the one that was set in the @Route("the_path") for that view.

All 7 comments

How would Router::getUrl work in the case of multiple route targets?
As a change having @Route(value = {"", "storefront"}) shouldn't be a big problem, but when you want to for instance get the url for the navigation target which route should be returned?
Does it matter?

This question was already discussed in the scope of the _old_ Router API. I guess, the conclusions are quite relevant still: https://github.com/vaadin/flow/issues/1476

I'd suggest introducing something like a repeatable @RouteAlias annotation with some suitable parameters (at least the same as @Route, but also some way of defining that value should be interpreted relative to the prefix of some other navigation target or router layout).

URL generation would thus always be based on @Route and @RouteAlias would only be used for supplementing the resolving functionality.

There could be an additional parameter @RouteAlias(value="/some/path", redirect=true) that will make Flow replace url in the browser to the one that was set in the @Route("the_path") for that view.

The last comment justifies the reason of having @RouteAlias annotation instead of making @Route repeatable.
But it should be done as a separate ticket #2870.

What happens if the route target is meant to support wildcard parameters but only on some URL patterns?

E.g given a route target like this, I want Flow to match the context root and "home/whatever" but not "profile/whatever"

@Route("")
@Route("home")
class MyRoute implements HasUrlParameter<String>

@sayo-vaadin A navigation target has one route and 0 - n aliases. If you want to make different aliases work in different ways, then you'd probably have to define those as distinct navigation targets that just happen to use the same contents.

Was this page helpful?
0 / 5 - 0 ratings