router-store icon indicating copy to clipboard operation
router-store copied to clipboard

Angular Router - navigation event fired twice

Open Noirox opened this issue 8 years ago • 13 comments

Hi,

when route is changed with Angulars Route, then the store will create new navigation event, which will result in two same router events.

For example:

<a [routerLink]="someLink">test me</a>

After you click that link, router will do the navigation, which is then pickup by the listener from connect.ts as stored state is different, but it will also call the Router to change location to the same url, resulting into two same navigation events.

Noirox avatar Nov 03 '16 10:11 Noirox

Has anyone looked into this? I believe I'm getting the same double firing behavior, any ideas for how to fix this?

petercn avatar Nov 18 '16 15:11 petercn

Can someone provide a simple reproduction of this issue?

brandonroberts avatar Nov 20 '16 04:11 brandonroberts

Same problem here, to reproduce just listen NavigationStart event:

this.router.events.filter(event => event instanceof NavigationStart).subscribe(() =>console.log('nav start'));

inzerceubytovani avatar Dec 25 '16 19:12 inzerceubytovani

This appears to still be happening. Is there any workaround (besides designing anything that listens to this to be able to fire twice without issue) or any plan to fix this?

dresdor avatar Mar 26 '18 15:03 dresdor

I came to this issue having a bug with the back button, that if I do this A->B->C and click back, it loops betwen C and B instead of going back to A. I am pretty sure it is related to this issue of the NavigationStart being fired twice. Any update?

aerlijman avatar Mar 28 '18 14:03 aerlijman

No updates. Version 2.x of @ngrx/router-store is no longer being maintained. You should upgrade as we have a full migration document from 2.x to 4.x(the same migration applies to 5.x which is the latest version).

brandonroberts avatar Mar 30 '18 13:03 brandonroberts

But this is still happening in Angular 5.x the one I have upgraded when I detected this thing. Any ideas? Are you still being able to duplicate it?

aerlijman avatar Apr 03 '18 12:04 aerlijman

@aerlijman can you share the code you are using to listen to the event? We discovered that if we use any of the events that get called by the routing process (ngOnInit, ngOnInitFinished, etc) that the method will get called once during the normal routing process and again when the listener hits

dresdor avatar Apr 03 '18 14:04 dresdor

In ngOnInit I have

        this.router.events.filter(event => event instanceof NavigationStart).subscribe(() => {
          
        });

aerlijman avatar Apr 03 '18 15:04 aerlijman

Hi, I am facing similar issue with ActivationEnd fired twice for single navigation (Angular 5, NGRX 4).

I have tried to isolate the issue here: https://github.com/felikf/angular-router-double-navigation

The interesting part is in router.effects.ts:

private listenToRouter() {
    this.router.events
        .filter(e => e instanceof ActivationEnd)
        .subscribe((event: ActivationEnd) => {

            // const url = event.snapshot._routerState.url
            this.logger.log(`Route Change: ${this.previousPath} => ${event.snapshot.routeConfig.path}`);

            this.store.dispatch(new routerActions.RouteChange({
                params: { ...event.snapshot.params },
                queryParams: { ...event.snapshot.queryParams },
                path: event.snapshot.routeConfig.path,
                previousPath: this.previousPath
            }));

            this.previousPath = event.snapshot.routeConfig.path;
        });
}

@brandonroberts please see this

felikf avatar Apr 05 '18 08:04 felikf

I got around this by making sure to unsubscribe at the appropriate times. in my case it was

onRouteChange() {
	this._routeListener = this.router.events.subscribe((event) => ...);
}

ngOnDestroy() {
	this._routeListener.unsubscribe();
}

ohabash avatar May 09 '18 17:05 ohabash

I'm seeing one subscription to router.events yield three calls to the callback function, and each reports instanceof event as ActivationEnd.

console.log('This code runs only once, at site initialization.');
router.events.subscribe((event: RouterEvent) => {
    if (event instanceof ActivationEnd) {
        console.log('This should only log once per route change, but logs three times.');
    };
});

Is this really a bug, or am I missing something?

BBaysinger avatar Jun 12 '18 02:06 BBaysinger

Oh. I figured out my problem: https://stackoverflow.com/a/50808849/1253298

I should have been using NavigationEnd, not ActivationEnd.

BBaysinger avatar Jun 12 '18 03:06 BBaysinger