fix(router): fix navigation ignoring logic to compare to the browser url (#37716)

This PR changes the logic for determining when to skip route processing from
using the URL of the last attempted navigation to the actual resulting URL after
that transition.

Because guards may prevent navigation and reset the browser URL, the raw
URL of the previous transition may not match the actual URL of the
browser at the end of the navigation process. For that reason, we need to use
`urlAfterRedirects` instead.

Other notes:
These checks in scheduleNavigation were added in eb2ceff4ba
The test still passes and, more surprisingly, passes if the checks are removed
completely. There have likely been changes to the navigation handling that
handle the test in a different way. That said, it still appears to be important
to keep the checks there in some capacity because it does affect how many
navigation events occur. This addresses an issue that came up in #16710: https://github.com/angular/angular/issues/16710#issuecomment-634869739
This also partially addresses #13586 in fixing history for imperative
navigations that are cancelled by guards.

PR Close #37716
This commit is contained in:
Andrew Scott
2020-06-02 15:55:38 -07:00
committed by Alex Rickabaugh
parent 196bfa8fae
commit a5ffca0576
2 changed files with 203 additions and 10 deletions

View File

@ -1126,12 +1126,28 @@ export class Router {
rawUrl: UrlTree, source: NavigationTrigger, restoredState: RestoredState|null,
extras: NavigationExtras,
priorPromise?: {resolve: any, reject: any, promise: Promise<boolean>}): Promise<boolean> {
// * Imperative navigations (router.navigate) might trigger additional navigations to the same
// URL via a popstate event and the locationChangeListener. We should skip these duplicate
// navs. Duplicates may also be triggered by attempts to sync AngularJS and Angular router
// states.
// * Imperative navigations can be cancelled by router guards, meaning the URL won't change. If
// the user follows that with a navigation using the back/forward button or manual URL change,
// the destination may be the same as the previous imperative attempt. We should not skip
// these navigations because it's a separate case from the one above -- it's not a duplicate
// navigation.
const lastNavigation = this.getTransition();
// If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
// and that navigation results in 'replaceState' that leads to the same URL,
// we should skip those.
if (lastNavigation && source !== 'imperative' && lastNavigation.source === 'imperative' &&
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
// We don't want to skip duplicate successful navs if they're imperative because
// onSameUrlNavigation could be 'reload' (so the duplicate is intended).
const browserNavPrecededByRouterNav =
source !== 'imperative' && lastNavigation?.source === 'imperative';
const lastNavigationSucceeded = this.lastSuccessfulId === lastNavigation.id;
// If the last navigation succeeded or is in flight, we can use the rawUrl as the comparison.
// However, if it failed, we should compare to the final result (urlAfterRedirects).
const lastNavigationUrl = (lastNavigationSucceeded || this.currentNavigation) ?
lastNavigation.rawUrl :
lastNavigation.urlAfterRedirects;
const duplicateNav = lastNavigationUrl.toString() === rawUrl.toString();
if (browserNavPrecededByRouterNav && duplicateNav) {
return Promise.resolve(true); // return value is not used
}