Skip to content

Commit 011fab3

Browse files
committed
fix(router): improve error for missing base href
Closes angular#3096
1 parent 8296dce commit 011fab3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

modules/angular2/src/router/location.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {LocationStrategy} from './location_strategy';
22
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
33
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
4+
import {BaseException, isBlank} from 'angular2/src/facade/lang';
45
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/di';
56

67
export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locationHrefToken'));
@@ -22,8 +23,14 @@ export class Location {
2223

2324
constructor(public _platformStrategy: LocationStrategy,
2425
@Optional() @Inject(appBaseHrefToken) href?: string) {
25-
this._baseHref = stripTrailingSlash(
26-
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
26+
var browserBaseHref = isPresent(href) ? href : this._platformStrategy.getBaseHref();
27+
28+
if (isBlank(browserBaseHref)) {
29+
throw new BaseException(
30+
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
31+
}
32+
33+
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
2734
this._platformStrategy.onPopState((_) => this._onPopState(_));
2835
}
2936

modules/angular2/test/router/location_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,13 @@ export function main() {
7575
location.go('user/btford');
7676
expect(locationStrategy.path()).toEqual('/my/custom/href/user/btford');
7777
});
78+
79+
it('should throw when no base href is provided', () => {
80+
var locationStrategy = new MockLocationStrategy();
81+
locationStrategy.internalBaseHref = null;
82+
expect(() => new Location(locationStrategy))
83+
.toThrowError(
84+
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
85+
});
7886
});
7987
}

0 commit comments

Comments
 (0)