Skip to content

Commit db461d6

Browse files
fix(browserLocation): Use location.pathname (not href) or '/' when no base tag found
1 parent bfa5755 commit db461d6

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/vanilla/browserLocationConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class BrowserLocationConfig implements LocationConfig {
4545

4646
applyDocumentBaseHref() {
4747
let baseTag: HTMLBaseElement = document.getElementsByTagName("base")[0];
48-
return this._baseHref = baseTag ? baseTag.href.substr(location.origin.length) : location.href;
48+
return this._baseHref = baseTag ? baseTag.href.substr(location.origin.length) : location.pathname || '/';
4949
}
5050

5151
dispose() {}

test/vanilla.browserLocationConfigSpec.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { UIRouter } from "../src/router";
2-
import { UrlService } from "../src/url/urlService";
1+
import { stripLastPathElement } from '../src/common';
2+
import { UIRouter } from '../src/router';
3+
import { UrlService } from '../src/url/urlService';
34
import * as vanilla from "../src/vanilla";
4-
import { UrlMatcherFactory } from "../src/url/urlMatcherFactory";
5+
import { UrlMatcherFactory } from '../src/url/urlMatcherFactory';
56
import { BrowserLocationConfig } from '../src/vanilla';
67
import { resetBrowserUrl } from './_testUtils';
78

89
describe('BrowserLocationConfig implementation', () => {
9-
afterAll(() => resetBrowserUrl())
10+
afterAll(() => resetBrowserUrl());
1011

1112
let router: UIRouter;
1213
let $url: UrlService;
@@ -62,14 +63,14 @@ describe('BrowserLocationConfig implementation', () => {
6263
expect(router.urlService.config.html5Mode()).toBe(true);
6364
let stub = spyOn(service._history, 'pushState');
6465
router.urlRouter.push($umf.compile('/hello/:name'), { name: 'world' }, {});
65-
expect(stub.calls.first().args[2]).toBe('/hello/world');
66+
expect(stub.calls.first().args[2]).toBe(stripLastPathElement($url.config.baseHref()) + '/hello/world');
6667
});
6768

6869
it('uses history.replaceState when setting a url with replace', () => {
6970
let service = mockPushState(router);
7071
let stub = spyOn(service._history, 'replaceState');
7172
router.urlRouter.push($umf.compile('/hello/:name'), { name: 'world' }, { replace: true });
72-
expect(stub.calls.first().args[2]).toBe('/hello/world');
73+
expect(stub.calls.first().args[2]).toBe(stripLastPathElement($url.config.baseHref()) + '/hello/world');
7374
});
7475

7576
it('returns the correct url query', async(done) => {
@@ -127,9 +128,9 @@ describe('BrowserLocationConfig implementation', () => {
127128
expect(blc.baseHref()).toBe('/base');
128129
});
129130

130-
it('uses location.href if <base> is not present', () => {
131+
it('uses location.pathname if <base> is not present', () => {
131132
const blc = new BrowserLocationConfig();
132-
expect(blc.baseHref()).toBe(location.href);
133+
expect(blc.baseHref()).toBe(location.pathname);
133134
});
134135
});
135136

0 commit comments

Comments
 (0)