Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ export class Router {
* way the next navigation will be coming from the current URL in the browser.
*/
this.rawUrlTree = t.rawUrl;
this.browserUrlTree = t.urlAfterRedirects;
t.resolve(null);
return EMPTY;
}
Expand Down Expand Up @@ -773,7 +774,14 @@ export class Router {
this.routerState.root.component = this.rootComponentType;
}

private getTransition(): NavigationTransition { return this.transitions.value; }
private getTransition(): NavigationTransition {
const transition = this.transitions.value;
// This value needs to be set. Other values such as extractedUrl are set on initial navigation
// but the urlAfterRedirects may not get set if we aren't processing the new URL *and* not
// processing the previous URL.
transition.urlAfterRedirects = this.browserUrlTree;
return transition;
}

private setTransition(t: Partial<NavigationTransition>): void {
this.transitions.next({...this.getTransition(), ...t});
Expand Down
60 changes: 60 additions & 0 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,39 @@ describe('Integration', () => {
})));
});


/**
* get/setTransition are private APIs. This test is needed though to guarantee the correct
* values are being used. Related to https://github.com/angular/angular/issues/30340 where
* stale transition data was being used when kicking off a new navigation.
*/
describe('get/setTransition', () => {
it('should provide the most recent NavigationTransition',
fakeAsync(inject([Router, Location], (router: Router, location: SpyLocation) => {
router.resetConfig([
{path: '', component: SimpleCmp}, {path: 'a', component: SimpleCmp},
{path: 'b', component: SimpleCmp}
]);

const fixture = createRoot(router, RootCmp);

const initialTransition = (router as any).getTransition();

// Confirm initial value
expect(initialTransition.urlAfterRedirects.toString()).toBe('/');


router.navigateByUrl('/a', {replaceUrl: true});

tick();

// After a navigation, we should see the URL after redirect
const nextTransition = (router as any).getTransition();
// Confirm initial value
expect(nextTransition.urlAfterRedirects.toString()).toBe('/a');
})));
});

describe('navigation warning', () => {
let warnings: string[] = [];

Expand Down Expand Up @@ -662,6 +695,33 @@ describe('Integration', () => {
expect(location.path()).toEqual('/login');
})));

it('should set browserUrlTree with urlUpdateStrategy="eagar" and false `shouldProcessUrl`',
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},
{path: 'login', component: AbsoluteSimpleLinkCmp}
]);

router.navigateByUrl('/team/22');
advance(fixture, 1);

expect((router as any).browserUrlTree.toString()).toBe('/team/22');

// Force to not process URL changes
router.urlHandlingStrategy.shouldProcessUrl = (url: UrlTree) => false;

router.navigateByUrl('/login');
advance(fixture, 1);

// Do not change locations
expect((router as any).browserUrlTree.toString()).toBe('/team/22');
})));

it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = TestBed.createComponent(RootCmp);
Expand Down