Platform: Dispatching events within a guard leads to loop in router-store

Created on 27 Jul 2017  路  9Comments  路  Source: ngrx/platform

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

What is the current behavior?

Dispatching an action synchronously in a guard leads to a loop.

Expected behavior:

No loop.

Minimal reproduction of the problem with instructions:


https://plnkr.co/edit/MNtybtGWdLg2e2dQu3EJ?p=preview

Watch the Console, while you click the link. NOTE: this causes a loop and will lag out your browser!

screen shot 2017-07-27 at 19 05 37

Analysis

Dispatching the action will lead to this part of the router store that will check if a navigation is necessary. However, this.router.url will point to the old route, while this.storeState['routerReducer'].state.url will point to the new route. Therefore, routing is cancelled and the same route is activated again, the guard is triggered again and then you have a loop.

Version of affected browser(s),operating system(s), npm, node and ngrx:

NPM 5.3
Browser: all
OS: all
node 6 LTS
ngrx 4.0.x

Router Store bug

Most helpful comment

Same is happening for me. An action with type: ROUTER_CANCEL is being dispatched, then the infinite loop begins.

All 9 comments

Same is happening for me. An action with type: ROUTER_CANCEL is being dispatched, then the infinite loop begins.

I had some problems with it. I resolved as I wrote it in the Issue #68.

However it should be nice to know which guard caused the cancellation to handle the redirecting properly.

@csutorasr this issue has nothing to do with guards cancelling anything as the guard just returns true.

Anyone have a workaround for this issue?

I kicked out router-store as it was not used very often in my project. The other "solution" I found was wrapping the dispatch in a timeout.

Both are not very solid solutions...

+1

There is another effect that is caused by the same mechanism:
If you have a canDeactivate guard and cancel route deactivation, then router-store will try to route you again.

A workaround for this issue is to change the name of the router slice to something other than routerReducer, like router. Router store only attempts to navigate when the state changes if the routerReducer key is present in the state and the URL doesn't match what's in the state.

Fixed via #355

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamal890 picture shyamal890  路  3Comments

RichardMisiak picture RichardMisiak  路  3Comments

oxiumio picture oxiumio  路  3Comments

brandonroberts picture brandonroberts  路  3Comments

hccampos picture hccampos  路  3Comments