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

Commit

Permalink
fix($location): don't crash if navigating outside the app base
Browse files Browse the repository at this point in the history
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
  • Loading branch information
petebacondarwin committed Jul 16, 2015
1 parent 92c7ce5 commit 9e492c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/ng/location.js
Expand Up @@ -907,6 +907,13 @@ function $LocationProvider() {

// update $location when $browser url changes
$browser.onUrlChange(function(newUrl, newState) {

if (isUndefined(beginsWith(appBaseNoFile, newUrl))) {
// If we are navigating outside of the app then force a reload
$window.location.href = newUrl;
return;
}

$rootScope.$evalAsync(function() {
var oldUrl = $location.absUrl();
var oldState = $location.$$state;
Expand Down
20 changes: 17 additions & 3 deletions test/ng/locationSpec.js
Expand Up @@ -860,7 +860,6 @@ describe('$location', function() {
});
});


// location.href = '...' fires hashchange event synchronously, so it might happen inside $apply
it('should not $apply when browser url changed inside $apply', function() {
initService({html5Mode:false,hashPrefix: '!',supportHistory: true});
Expand Down Expand Up @@ -1150,6 +1149,19 @@ describe('$location', function() {
expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b/bar', false, null]);
});
});

it('should force a page reload if navigating outside of the application base href', function() {
initService({html5Mode:true, supportHistory: true});
mockUpBrowser({initialUrl:'http://new.com/a/b/', baseHref:'/a/b/'});

inject(function($window, $browser, $location) {
$window.location.href = 'http://new.com/a/outside.html';
spyOn($window.location, '$$setHref');
expect($window.location.$$setHref).not.toHaveBeenCalled();
$browser.$$checkUrlChange();
expect($window.location.$$setHref).toHaveBeenCalledWith('http://new.com/a/outside.html');
});
});
});


Expand Down Expand Up @@ -2540,8 +2552,10 @@ describe('$location', function() {
win.addEventListener = angular.noop;
win.removeEventListener = angular.noop;
win.location = {
get href() { return parser.href; },
set href(val) { parser.href = val; },
get href() { return this.$$getHref(); },
$$getHref: function() { return parser.href; },
set href(val) { this.$$setHref(val); },
$$setHref: function(val) { parser.href = val; },
get hash() { return parser.hash; },
// The parser correctly strips on a single preceding hash character if necessary
// before joining the fragment onto the href by a new hash character
Expand Down

0 comments on commit 9e492c3

Please sign in to comment.