Nx: DataPersistence.navigation is called while the parent component has not been activated yet.

Created on 6 Feb 2018  路  3Comments  路  Source: nrwl/nx

I have the following route configuration:

const ROUTES = [
  {
    path: '',
    component: ContainerComponent,
    children: [
      { path: 'home', component: HomePageComponent }
    ],
    canActivate: [UserConnectedGuard]
  }
]

and the following effect:

@Effect()
loadProtectedData = this._dataPersistence.navigation(HomePageComponent, {
  run: (route: ActivatedRouteSnapshot, state: AppState) => {
    return this._backend.getProtectedData().pipe(
      map((data) => ({
        type: 'DATA_LOADED',
        payload: data
      }))
    );
  },

  onError: (route: ActivatedRouteSnapshot, error) => {}
});

When navigating to /home, the effect loadProtectedData is called while the UserConnectedGuard has not yet been resolved. Is that the expected behavior or do I miss something? Does anybody have an idea about the ideal architecture to solve that kind of dependency?

BTW, great work so far! Thanks guys.

bug

Most helpful comment

DataPersistence.navigation relies on the NgRx router integration. The integration dispatches the navigation event before the preactivation phase (Guards and Resolvers) executes, and that's why you are seeing the issue.

Could you submit an issue to https://github.com/ngrx/platform? I'll fix it in the NgRx repo.

Since the problem is caused by NgRx, I'm closing this issue.

All 3 comments

DataPersistence.navigation relies on the NgRx router integration. The integration dispatches the navigation event before the preactivation phase (Guards and Resolvers) executes, and that's why you are seeing the issue.

Could you submit an issue to https://github.com/ngrx/platform? I'll fix it in the NgRx repo.

Since the problem is caused by NgRx, I'm closing this issue.

Just ran into this issue. I searched ngrx bugs bug couldn't find an entry there, so I opened one up using the information in this thread.

https://github.com/ngrx/platform/issues/975

Thanks, :)

For anyone who happens to find this issue via Google (like me) the solution to stop this from happening is to update your StoreRouterConnectingModule as follows:

    StoreRouterConnectingModule.forRoot({
      navigationActionTiming: NavigationActionTiming.PostActivation
    })

This ensures that the ROUTER_NAVIGATION actions that Nx looks for are only dispatched after guards have resolved.

This could also be done at the Nx level by instead listening for the ROUTER_NAVIGATED action instead of ROUTER_NAVIGATION but this might be a breaking change for some.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dereklin picture dereklin  路  3Comments

Svancara picture Svancara  路  3Comments

about-code picture about-code  路  3Comments

zpydee picture zpydee  路  3Comments

danieldanielecki picture danieldanielecki  路  3Comments