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

Commit 623ce1a

Browse files
fix($location): don't crash if navigating outside the app base
Previously, if you navigate outside of the Angular application, say be clicking the back button, the $location service would try to handle the url change and error due to the URL not being valid for the application. This fixes that issue by ensuring that a reload happens when you navigate to a URL that is not within the application. Closes #11667
1 parent 34cf141 commit 623ce1a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/ng/location.js

+7
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,13 @@ function $LocationProvider() {
904904

905905
// update $location when $browser url changes
906906
$browser.onUrlChange(function(newUrl, newState) {
907+
908+
if (isUndefined(beginsWith(appBaseNoFile, newUrl))) {
909+
// If we are navigating outside of the app then force a reload
910+
$window.location.href = newUrl;
911+
return;
912+
}
913+
907914
$rootScope.$evalAsync(function() {
908915
var oldUrl = $location.absUrl();
909916
var oldState = $location.$$state;

test/ng/locationSpec.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,6 @@ describe('$location', function() {
860860
});
861861
});
862862

863-
864863
// location.href = '...' fires hashchange event synchronously, so it might happen inside $apply
865864
it('should not $apply when browser url changed inside $apply', function() {
866865
initService({html5Mode:false,hashPrefix: '!',supportHistory: true});
@@ -1150,6 +1149,19 @@ describe('$location', function() {
11501149
expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b/bar', false, null]);
11511150
});
11521151
});
1152+
1153+
it('should force a page reload if navigating outside of the application base href', function() {
1154+
initService({html5Mode:true, supportHistory: true});
1155+
mockUpBrowser({initialUrl:'http://new.com/a/b/', baseHref:'/a/b/'});
1156+
1157+
inject(function($window, $browser, $location) {
1158+
$window.location.href = 'http://new.com/a/outside.html';
1159+
spyOn($window.location, '$$setHref');
1160+
expect($window.location.$$setHref).not.toHaveBeenCalled();
1161+
$browser.$$checkUrlChange();
1162+
expect($window.location.$$setHref).toHaveBeenCalledWith('http://new.com/a/outside.html');
1163+
});
1164+
});
11531165
});
11541166

11551167

@@ -2540,8 +2552,10 @@ describe('$location', function() {
25402552
win.addEventListener = angular.noop;
25412553
win.removeEventListener = angular.noop;
25422554
win.location = {
2543-
get href() { return parser.href; },
2544-
set href(val) { parser.href = val; },
2555+
get href() { return this.$$getHref(); },
2556+
$$getHref: function() { return parser.href; },
2557+
set href(val) { this.$$setHref(val); },
2558+
$$setHref: function(val) { parser.href = val; },
25452559
get hash() { return parser.hash; },
25462560
// The parser correctly strips on a single preceding hash character if necessary
25472561
// before joining the fragment onto the href by a new hash character

0 commit comments

Comments
 (0)