Skip to content

Commit c64c252

Browse files
fix(state): Update URL in response to ignored transition due to redirect
1 parent 8ed691b commit c64c252

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/state/stateService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export class StateService {
349349
*/
350350
const rejectedTransitionHandler = (trans: Transition) => (error: any): Promise<any> => {
351351
if (error instanceof Rejection) {
352-
const isLatest = router.globals.lastStartedTransitionId === trans.$id;
352+
const isLatest = router.globals.lastStartedTransitionId <= trans.$id;
353353

354354
if (error.type === RejectType.IGNORED) {
355355
isLatest && router.urlRouter.update();

test/transitionSpec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,41 @@ describe('transition', function() {
955955
router.urlService.url('/urlRedirect');
956956
});
957957

958+
it('should replace the current url when redirecting a url sync transition, even if the redirect is ignored', async done => {
959+
router.stateRegistry.register({
960+
name: 'queryparam',
961+
url: '/?param',
962+
});
963+
964+
await router.stateService.go('queryparam', { param: undefined });
965+
expect(router.globals.params.param).toEqual(undefined);
966+
967+
router.transitionService.onStart({}, trans => {
968+
// redirect transition removing ?param=foo
969+
if (trans.params().param === 'foo') {
970+
return trans.targetState().withParams({ param: undefined });
971+
}
972+
});
973+
974+
router.urlService.url('/?param=foo');
975+
const url = spyOn(router.locationService, 'url').and.callThrough();
976+
const update = spyOn(router.urlRouter, 'update').and.callThrough();
977+
978+
router.transitionService.onError({}, trans => {
979+
if (trans.error().type === RejectType.IGNORED) {
980+
setTimeout(() => {
981+
expect(update.calls.count()).toBe(1);
982+
expect(url.calls.count()).toBe(2);
983+
expect(url.calls.argsFor(0)).toEqual([]);
984+
expect(url.calls.argsFor(1)).toEqual(['/', true]);
985+
expect(router.urlService.url()).toBe('/');
986+
expect(router.globals.params.param).toEqual(undefined);
987+
done();
988+
});
989+
}
990+
});
991+
});
992+
958993
it('should not replace the current url when redirecting a url sync with { location: false }', done => {
959994
router.transitionService.onBefore({ to: 'requiresAuth' }, trans => {
960995
return router.stateService.target('redirectTarget', null, { location: false });

0 commit comments

Comments
 (0)