Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7f4b356

Browse files
jardiliopetebacondarwin
authored andcommitted
fix($route): don't process route change controllers and templates for redirectTo routes
If a route defines a `redirectTo` value, the current route should stop processing the route and instead wait to process the route of the redirect. Currently, what is happening is that Angular detects a redirectTo value and updates $location, but then continues processing the route that was intended to be redirected. Templates are loaded, resolves are processed, ng-view then updates the view with a new template and instantiates the controller all for a route that should have been redirected. A common use case for assigning a function to the redirectTo is to validation authentication, permission check, or some other logic to determine if the route should continue or redirect else where. In the end, this happens, but in between, unexpected results may occur by updating the view and instantiating controllers for logic that wasn't expected to be executed. http://plnkr.co/edit/8QlA0ouuePH3p35Ntmjy?p=preview This commit checks for a url change after a potential redirect, it the url changed, and is not `null` nor `undefined`, exit out and don't process current route change. Closes #3332 Closes #14658 BREAKING CHANGE The $route service no longer instantiates controllers nor calls resolves or template functions for routes that have a `redirectTo` unless the `redirectTo` is a function that returns `undefined`.
1 parent 4c95ad8 commit 7f4b356

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/ngRoute/route.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ function $RouteProvider() {
136136
* The custom `redirectTo` function is expected to return a string which will be used
137137
* to update `$location.path()` and `$location.search()`.
138138
*
139+
* Routes that specify `redirectTo` will not have their controllers, template functions
140+
* or resolves called, the `$location` will be changed to the redirect url and route
141+
* processing will stop. The exception to this is if the `redirectTo` is a function that
142+
* returns `undefined`. In this case the route transition occurs as though there was no
143+
* redirection.
144+
*
139145
* - `[reloadOnSearch=true]` - `{boolean=}` - reload route when only `$location.search()`
140146
* or `$location.hash()` changes.
141147
*
@@ -588,12 +594,19 @@ function $RouteProvider() {
588594
$route.current = nextRoute;
589595
if (nextRoute) {
590596
if (nextRoute.redirectTo) {
597+
var url = $location.url();
598+
var newUrl;
591599
if (angular.isString(nextRoute.redirectTo)) {
592-
$location.path(interpolate(nextRoute.redirectTo, nextRoute.params)).search(nextRoute.params)
600+
$location.path(interpolate(nextRoute.redirectTo, nextRoute.params))
601+
.search(nextRoute.params)
593602
.replace();
603+
newUrl = $location.url();
594604
} else {
595-
$location.url(nextRoute.redirectTo(nextRoute.pathParams, $location.path(), $location.search()))
596-
.replace();
605+
newUrl = nextRoute.redirectTo(nextRoute.pathParams, $location.path(), $location.search());
606+
$location.url(newUrl).replace();
607+
}
608+
if (angular.isDefined(newUrl) && url !== newUrl) {
609+
return; //exit out and don't process current next value, wait for next location change from redirect
597610
}
598611
}
599612
}

test/ngRoute/routeSpec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,64 @@ describe('$route', function() {
10611061
.toEqual(['http://server/#!/bar/id3?extra=eId', true, null]);
10621062
});
10631063
});
1064+
1065+
it('should not process route bits', function() {
1066+
var firstController = jasmine.createSpy('first controller spy');
1067+
var firstTemplate = jasmine.createSpy('first template spy').and.returnValue('redirected view');
1068+
var firstResolve = jasmine.createSpy('first resolve spy');
1069+
var secondController = jasmine.createSpy('second controller spy');
1070+
var secondTemplate = jasmine.createSpy('second template spy').and.returnValue('redirected view');
1071+
var secondResolve = jasmine.createSpy('second resolve spy');
1072+
module(function($routeProvider) {
1073+
$routeProvider.when('/redirect', {
1074+
template: firstTemplate,
1075+
redirectTo: '/redirected',
1076+
resolve: { value: firstResolve },
1077+
controller: firstController
1078+
});
1079+
$routeProvider.when('/redirected', {
1080+
template: secondTemplate,
1081+
resolve: { value: secondResolve },
1082+
controller: secondController
1083+
});
1084+
});
1085+
inject(function($route, $location, $rootScope, $compile) {
1086+
var element = $compile('<div><ng-view></ng-view></div>')($rootScope);
1087+
$location.path('/redirect');
1088+
$rootScope.$digest();
1089+
1090+
expect(firstController).not.toHaveBeenCalled();
1091+
expect(firstTemplate).not.toHaveBeenCalled();
1092+
expect(firstResolve).not.toHaveBeenCalled();
1093+
1094+
expect(secondController).toHaveBeenCalled();
1095+
expect(secondTemplate).toHaveBeenCalled();
1096+
expect(secondResolve).toHaveBeenCalled();
1097+
1098+
dealoc(element);
1099+
});
1100+
});
1101+
1102+
it('should not redirect transition if `redirectTo` returns `undefined`', function() {
1103+
var controller = jasmine.createSpy('first controller spy');
1104+
var templateFn = jasmine.createSpy('first template spy').and.returnValue('redirected view');
1105+
module(function($routeProvider) {
1106+
$routeProvider.when('/redirect/to/undefined', {
1107+
template: templateFn,
1108+
redirectTo: function() {},
1109+
controller: controller
1110+
});
1111+
});
1112+
inject(function($route, $location, $rootScope, $compile) {
1113+
var element = $compile('<div><ng-view></ng-view></div>')($rootScope);
1114+
$location.path('/redirect/to/undefined');
1115+
$rootScope.$digest();
1116+
expect(controller).toHaveBeenCalled();
1117+
expect(templateFn).toHaveBeenCalled();
1118+
expect($location.path()).toEqual('/redirect/to/undefined');
1119+
dealoc(element);
1120+
});
1121+
});
10641122
});
10651123

10661124

0 commit comments

Comments
 (0)