question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Navigating to an Angular Route from AngularJS (Hybrid) runs Resolvers and Guards twice

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

Yes

Description

#45240 Specifically removed some logic that was responsible for preventing duplicate navigation in hybrid apps, stating that

There have been many updates to the routing pipeline to tolerate duplicate navigations. That is, duplicate navigations can happen and routing should still complete successfully.

The problem with this is that, when this duplicate navigation occurs, all angular resolves and guards are running twice for the route. As a result of upgrading to @angular/router@13.2.6, all of our resolves are running twice, making duplicate server requests for many of our pages. In one instance where this was found, it actually completely broke navigation as the second network request of the same payload will always error out.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

@angular/router: 13.2.6

Anything else?

I was quite shocked that this change specifically was not at all documented in the changelog for 13.2.6, as this seems very breaking to me. Even worse, it’s included in a patch. This code was obviously added for some reason, so it’s weird that it just got deleted in favor of “double navigation being okay.”

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
atscottcommented, Jun 21, 2022

b9aab0c went to 13.2.x and 13.3.x. Without knowing the details of your application hybrid navigations, it’s hard to debug your specific use-case. I would guess that you have some app-specific hybrid routing to sync the locations, but I don’t know for sure.

We have a special href directive that is keeping the href attribute up to date on the anchor element. It builds up the href via Router.serializeUrl(Router.createUrlTree(commands, extras)) and appends a hash in the front if necessary.

Ah, yea that’s probably what’s doing it and I would say this is working as intended, more than the old behavior. Setting the href triggers a popstate event, which would cause the Angular router to view this as a URL change that needs to be synced to its internal state. One option would be to delay the href update so that the Router finishes activation by waiting for something like the NavigationEnd event (this is probably the best option – generally you don’t want to navigate until the current navigation finishes). Then it’ll skip the syncing navigation because of this bit of code. Another option would be to ensure that your directive only sets the href if it would be different than what’s currently in the browser URL bar (not sure if you already do this).

1reaction
atscottcommented, Jun 21, 2022

https://github.com/angular/angular/commit/b9aab0c87bcccb61167e92c1e910630afad67648 went to 13.2.x and 13.3.x. Without knowing the details of your application hybrid navigations, it’s hard to debug your specific use-case. I would guess that you have some app-specific hybrid routing to sync the locations, but I don’t know for sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJS UI router's "resolve" fires twice - Stack Overflow
Just from reading I assume the issue is with a state change in the ReaderCtrl and it is rerendering. It then resolves the...
Read more >
Common Routing Tasks - Angular
The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is...
Read more >
Sharing the UI router between AngularJS and Angular 6
Start with the hybrid routes when you start to convert entire pages because otherwise, you'll be doing twice the work in the long...
Read more >
Hybrid apps using both routers can instantiate components ...
navigating to one such component. The component is instantiated twice, which leads to constructor logic being ran twice, and also seems to break ......
Read more >
Running Angular Guards Synchronously - Upmostly
I had a scenario where I had two route guards. The first was simply to check if the user was logged in via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found