platform icon indicating copy to clipboard operation
platform copied to clipboard

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

Open PerfectPixel opened this issue 9 years ago • 9 comments

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

PerfectPixel avatar Jul 27 '17 17:07 PerfectPixel

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

chrisbenseler avatar Jul 27 '17 17:07 chrisbenseler

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 avatar Jul 30 '17 23:07 csutorasr

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

PerfectPixel avatar Jul 31 '17 09:07 PerfectPixel

Anyone have a workaround for this issue?

wesselvdv avatar Aug 18 '17 08:08 wesselvdv

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...

PerfectPixel avatar Aug 18 '17 08:08 PerfectPixel

+1

skkonstantin avatar Aug 24 '17 10:08 skkonstantin

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.

PerfectPixel avatar Aug 28 '17 08:08 PerfectPixel

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.

brandonroberts avatar Sep 04 '17 04:09 brandonroberts

Fixed via #355

brandonroberts avatar Sep 07 '17 02:09 brandonroberts