Enabling initialNavigation with LocalizeRouter and Angular Universal

V
Jul 18, 2019

--

When I set up a Localization Router, I came across a problem with initialNavigation. When it’s enabled, every localized URL falls with an error:

Can not match any routes. URL segment: en/profile

This happened because LocalizeRouter wasn’t initialized yet and the initial segment of the url (language shortening — en) was perceived as part of the route.

After hours of researching, I found a solution that works for me — call init method on LocalizeRouterService when app loads. This is solved by simply adding the APP_INITIALIZER variable to the app-routing providers:

I used Injector because with the direct addition of LocalizeRouterService there was a cyclic dependency error.

Full code below:

Thanks for reading! I hope, it’ll help someone ❤

--

--