Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
PerfectPixel opened this issue Jul 27, 2017 · 9 comments
Closed

Comments

@PerfectPixel
Copy link

PerfectPixel commented Jul 27, 2017

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

@chrisbenseler
Copy link

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

@csutorasr
Copy link

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.

@PerfectPixel
Copy link
Author

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

@wesselvdv
Copy link

Anyone have a workaround for this issue?

@PerfectPixel
Copy link
Author

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

@skkonstantin
Copy link

skkonstantin commented Aug 24, 2017

+1

@PerfectPixel
Copy link
Author

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.

@brandonroberts
Copy link
Member

brandonroberts commented Sep 4, 2017

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
Copy link
Member

Fixed via #355

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants