Revert "revert: fix(router): ensure URL is updated after second redirect with UrlUpdateStrategy="eager" (#27523)" (#28300)

This reverts commit eea2b0f288eec889d56afb07dad21f75e77f1241.

PR Close #28300
This commit is contained in:
Jason Aden 2019-01-22 13:48:00 -08:00 committed by Alex Rickabaugh
parent ea1b5c100f
commit 33e49c2894
2 changed files with 110 additions and 50 deletions

View File

@ -291,6 +291,7 @@ function defaultRouterHook(snapshot: RouterStateSnapshot, runExtras: {
export class Router { export class Router {
private currentUrlTree: UrlTree; private currentUrlTree: UrlTree;
private rawUrlTree: UrlTree; private rawUrlTree: UrlTree;
private browserUrlTree: UrlTree;
private readonly transitions: BehaviorSubject<NavigationTransition>; private readonly transitions: BehaviorSubject<NavigationTransition>;
private navigations: Observable<NavigationTransition>; private navigations: Observable<NavigationTransition>;
private lastSuccessfulNavigation: Navigation|null = null; private lastSuccessfulNavigation: Navigation|null = null;
@ -400,6 +401,7 @@ export class Router {
this.resetConfig(config); this.resetConfig(config);
this.currentUrlTree = createEmptyUrlTree(); this.currentUrlTree = createEmptyUrlTree();
this.rawUrlTree = this.currentUrlTree; this.rawUrlTree = this.currentUrlTree;
this.browserUrlTree = this.parseUrl(this.location.path());
this.configLoader = new RouterConfigLoader(loader, compiler, onLoadStart, onLoadEnd); this.configLoader = new RouterConfigLoader(loader, compiler, onLoadStart, onLoadEnd);
this.routerState = createEmptyState(this.currentUrlTree, this.rootComponentType); this.routerState = createEmptyState(this.currentUrlTree, this.rootComponentType);
@ -461,7 +463,7 @@ export class Router {
return of (t).pipe( return of (t).pipe(
switchMap(t => { switchMap(t => {
const urlTransition = const urlTransition =
!this.navigated || t.extractedUrl.toString() !== this.currentUrlTree.toString(); !this.navigated || t.extractedUrl.toString() !== this.browserUrlTree.toString();
const processCurrentUrl = const processCurrentUrl =
(this.onSameUrlNavigation === 'reload' ? true : urlTransition) && (this.onSameUrlNavigation === 'reload' ? true : urlTransition) &&
this.urlHandlingStrategy.shouldProcessUrl(t.rawUrl); this.urlHandlingStrategy.shouldProcessUrl(t.rawUrl);
@ -502,8 +504,12 @@ export class Router {
this.paramsInheritanceStrategy, this.relativeLinkResolution), this.paramsInheritanceStrategy, this.relativeLinkResolution),
// Update URL if in `eager` update mode // Update URL if in `eager` update mode
tap(t => this.urlUpdateStrategy === 'eager' && !t.extras.skipLocationChange && tap(t => {
this.setBrowserUrl(t.urlAfterRedirects, !!t.extras.replaceUrl, t.id)), if (this.urlUpdateStrategy === 'eager' && !t.extras.skipLocationChange) {
this.setBrowserUrl(t.urlAfterRedirects, !!t.extras.replaceUrl, t.id);
this.browserUrlTree = t.urlAfterRedirects;
}
}),
// Fire RoutesRecognized // Fire RoutesRecognized
tap(t => { tap(t => {
@ -665,6 +671,7 @@ export class Router {
if (this.urlUpdateStrategy === 'deferred' && !t.extras.skipLocationChange) { if (this.urlUpdateStrategy === 'deferred' && !t.extras.skipLocationChange) {
this.setBrowserUrl(this.rawUrlTree, !!t.extras.replaceUrl, t.id, t.extras.state); this.setBrowserUrl(this.rawUrlTree, !!t.extras.replaceUrl, t.id, t.extras.state);
this.browserUrlTree = t.urlAfterRedirects;
} }
}), }),

View File

@ -12,7 +12,7 @@ import {ChangeDetectionStrategy, Component, Injectable, NgModule, NgModuleFactor
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser/src/dom/debug/by'; import {By} from '@angular/platform-browser/src/dom/debug/by';
import {expect} from '@angular/platform-browser/testing/src/matchers'; import {expect} from '@angular/platform-browser/testing/src/matchers';
import {ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, CanActivate, CanDeactivate, ChildActivationEnd, ChildActivationStart, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, Navigation, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterEvent, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlSerializer, UrlTree} from '@angular/router'; import {ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, ActivationStart, CanActivate, CanDeactivate, ChildActivationEnd, ChildActivationStart, DefaultUrlSerializer, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, Navigation, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterEvent, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlSerializer, UrlTree} from '@angular/router';
import {Observable, Observer, Subscription, of } from 'rxjs'; import {Observable, Observer, Subscription, of } from 'rxjs';
import {filter, first, map, tap} from 'rxjs/operators'; import {filter, first, map, tap} from 'rxjs/operators';
@ -571,6 +571,21 @@ describe('Integration', () => {
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]'); expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
}))); })));
describe('"eager" urlUpdateStrategy', () => {
beforeEach(() => {
const serializer = new DefaultUrlSerializer();
TestBed.configureTestingModule({
providers: [{
provide: 'authGuardFail',
useValue: (a: any, b: any) => {
return new Promise(res => { setTimeout(() => res(serializer.parse('/login')), 1); });
}
}]
});
});
it('should eagerly update the URL with urlUpdateStrategy="eagar"', it('should eagerly update the URL with urlUpdateStrategy="eagar"',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = TestBed.createComponent(RootCmp); const fixture = TestBed.createComponent(RootCmp);
@ -595,6 +610,35 @@ describe('Integration', () => {
advance(fixture); advance(fixture);
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]'); expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
}))); })));
it('should eagerly update the URL with urlUpdateStrategy="eagar"',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = TestBed.createComponent(RootCmp);
advance(fixture);
router.urlUpdateStrategy = 'eager';
router.resetConfig([
{path: 'team/:id', component: SimpleCmp, canActivate: ['authGuardFail']},
{path: 'login', component: AbsoluteSimpleLinkCmp}
]);
router.navigateByUrl('/team/22');
advance(fixture);
expect(location.path()).toEqual('/team/22');
// Redirects to /login
advance(fixture, 1);
expect(location.path()).toEqual('/login');
// Perform the same logic again, and it should produce the same result
router.navigateByUrl('/team/22');
advance(fixture);
expect(location.path()).toEqual('/team/22');
// Redirects to /login
advance(fixture, 1);
expect(location.path()).toEqual('/login');
})));
it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"', it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
@ -630,6 +674,8 @@ describe('Integration', () => {
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]'); expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
}))); })));
});
it('should navigate back and forward', it('should navigate back and forward',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
@ -4656,6 +4702,10 @@ class DummyLinkCmp {
} }
} }
@Component({selector: 'link-cmp', template: `<a [routerLink]="['/simple']">link</a>`})
class AbsoluteSimpleLinkCmp {
}
@Component({selector: 'link-cmp', template: `<a [routerLink]="['../simple']">link</a>`}) @Component({selector: 'link-cmp', template: `<a [routerLink]="['../simple']">link</a>`})
class RelativeLinkCmp { class RelativeLinkCmp {
} }
@ -4836,8 +4886,8 @@ class ThrowingCmp {
function advance(fixture: ComponentFixture<any>): void { function advance(fixture: ComponentFixture<any>, millis?: number): void {
tick(); tick(millis);
fixture.detectChanges(); fixture.detectChanges();
} }
@ -4865,6 +4915,7 @@ class LazyComponent {
StringLinkCmp, StringLinkCmp,
DummyLinkCmp, DummyLinkCmp,
AbsoluteLinkCmp, AbsoluteLinkCmp,
AbsoluteSimpleLinkCmp,
RelativeLinkCmp, RelativeLinkCmp,
DummyLinkWithParentCmp, DummyLinkWithParentCmp,
LinkWithQueryParamsAndFragment, LinkWithQueryParamsAndFragment,
@ -4893,6 +4944,7 @@ class LazyComponent {
StringLinkCmp, StringLinkCmp,
DummyLinkCmp, DummyLinkCmp,
AbsoluteLinkCmp, AbsoluteLinkCmp,
AbsoluteSimpleLinkCmp,
RelativeLinkCmp, RelativeLinkCmp,
DummyLinkWithParentCmp, DummyLinkWithParentCmp,
LinkWithQueryParamsAndFragment, LinkWithQueryParamsAndFragment,
@ -4923,6 +4975,7 @@ class LazyComponent {
StringLinkCmp, StringLinkCmp,
DummyLinkCmp, DummyLinkCmp,
AbsoluteLinkCmp, AbsoluteLinkCmp,
AbsoluteSimpleLinkCmp,
RelativeLinkCmp, RelativeLinkCmp,
DummyLinkWithParentCmp, DummyLinkWithParentCmp,
LinkWithQueryParamsAndFragment, LinkWithQueryParamsAndFragment,