Skip to content

Commit 042a950

Browse files
fix(pushStateLocation): When url is "" or "/", use baseHref for pushState
1 parent db461d6 commit 042a950

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/vanilla/pushStateLocationService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class PushStateLocationService extends BaseLocationServices {
5757
protected _set(state: any, title: string, url: string, replace: boolean) {
5858
const basePrefix = this._getBasePrefix();
5959
const slash = url && url[0] !== '/' ? '/' : '';
60-
const fullUrl = url === '' ? this._config.baseHref() : basePrefix + slash + url;
60+
const fullUrl = (url === '' || url === '/') ? this._config.baseHref() : basePrefix + slash + url;
6161

6262
if (replace) {
6363
this._history.replaceState(state, title, fullUrl);

test/vanilla.pushStateLocationServiceSpec.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ describe('pushStateLocationService', () => {
4040
expect($url.search()).toEqual({});
4141
});
4242

43-
it('sets and returns an empty path', () => {
43+
it('returns a slash when an empty path is set', () => {
44+
const base = $url.config.baseHref();
4445
$url.url('');
45-
expect(window.location.pathname).toBe('');
46-
expect($url.path()).toBe('');
46+
expect(window.location.pathname).toBe(base);
47+
expect($url.path()).toBe('/');
4748
});
4849

4950
it('sets and returns a path with a single slash', () => {
51+
const base = $url.config.baseHref();
5052
$url.url('/');
51-
expect(window.location.pathname).toBe('/');
53+
expect(window.location.pathname).toBe(base);
5254
expect($url.path()).toBe('/');
5355
});
5456

@@ -60,7 +62,7 @@ describe('pushStateLocationService', () => {
6062
expect($url.search()).toEqual({ queryParam: 'query' });
6163
});
6264

63-
fdescribe('with base tag', () => {
65+
describe('with base tag', () => {
6466
let baseTag: HTMLBaseElement;
6567
const applyBaseTag = (href: string) => {
6668
baseTag = document.createElement('base');
@@ -71,55 +73,57 @@ describe('pushStateLocationService', () => {
7173
afterEach(() => baseTag.parentElement.removeChild(baseTag));
7274

7375
describe('/base/', () => {
74-
beforeEach(() => applyBaseTag("/base/"));
76+
const base = '/base/';
77+
beforeEach(() => applyBaseTag(base));
7578

7679
it('reports html5Mode to be true', () => {
7780
expect(router.urlService.config.html5Mode()).toBe(true);
7881
});
7982

8083
it('handles bar correctly', () => {
8184
$url.url('bar');
82-
expect(window.location.pathname).toBe('/base/bar');
85+
expect(window.location.pathname).toBe(`${base}bar`);
8386
expect($url.path()).toBe('/bar');
8487
});
8588

8689
it('handles /bar correctly', () => {
8790
$url.url('/bar');
88-
expect(window.location.pathname).toBe('/base/bar');
91+
expect(window.location.pathname).toBe(`${base}bar`);
8992
expect($url.path()).toBe('/bar');
9093
});
9194

9295
it('handles /path/bar correctly', () => {
9396
$url.url('/path/bar');
94-
expect(window.location.pathname).toBe('/base/path/bar');
97+
expect(window.location.pathname).toBe(`${base}path/bar`);
9598
expect($url.path()).toBe('/path/bar');
9699
});
97100

98101
it('handles / correctly', () => {
99102
$url.url('/');
100-
expect(window.location.pathname).toBe('/base/');
103+
expect(window.location.pathname).toBe(base);
101104
expect($url.path()).toBe('/');
102105
});
103106

104107
it('handles "" correctly', () => {
105108
$url.url('foobar');
106-
expect(window.location.pathname).toBe('/base/foobar');
109+
expect(window.location.pathname).toBe(`${base}foobar`);
107110
$url.url('');
108-
expect(window.location.pathname).toBe('/base/');
111+
expect(window.location.pathname).toBe(base);
109112
expect($url.path()).toBe('/');
110113
});
111114

112115
it('handles ?queryParam=query correctly', () => {
113116
$url.url('/path/bar?queryParam=query');
114-
expect(window.location.pathname).toBe('/base/path/bar');
117+
expect(window.location.pathname).toBe(`${base}path/bar`);
115118
expect(window.location.search).toBe('?queryParam=query');
116119
expect($url.path()).toBe('/path/bar');
117120
expect($url.search()).toEqual({ queryParam: 'query' });
118121
});
119122
});
120123

121124
describe('/debug.html', () => {
122-
beforeEach(() => applyBaseTag("/debug.html"));
125+
const base = "/debug.html";
126+
beforeEach(() => applyBaseTag(base));
123127

124128
it('handles bar correctly', () => {
125129
$url.url('bar');
@@ -141,15 +145,15 @@ describe('pushStateLocationService', () => {
141145

142146
it('handles / correctly', () => {
143147
$url.url('/');
144-
expect(window.location.pathname).toBe('/debug.html');
148+
expect(window.location.pathname).toBe(base);
145149
expect($url.path()).toBe('/');
146150
});
147151

148152
it('handles "" correctly', () => {
149153
$url.url('foobar');
150154
expect(window.location.pathname).toBe('/foobar');
151155
$url.url('');
152-
expect(window.location.pathname).toBe('/debug.html');
156+
expect(window.location.pathname).toBe(base);
153157
expect($url.path()).toBe('/');
154158
});
155159

@@ -163,7 +167,8 @@ describe('pushStateLocationService', () => {
163167
});
164168

165169
describe(origin + '/debug.html', () => {
166-
beforeEach(() => applyBaseTag(origin + '/debug.html'));
170+
const base = '/debug.html';
171+
beforeEach(() => applyBaseTag(origin + base));
167172

168173
it('handles bar correctly', () => {
169174
$url.url('bar');
@@ -185,15 +190,15 @@ describe('pushStateLocationService', () => {
185190

186191
it('handles / correctly', () => {
187192
$url.url('/');
188-
expect(window.location.pathname).toBe('/debug.html');
193+
expect(window.location.pathname).toBe(base);
189194
expect($url.path()).toBe('/');
190195
});
191196

192197
it('handles "" correctly', () => {
193198
$url.url('foobar');
194199
expect(window.location.pathname).toBe('/foobar');
195200
$url.url('');
196-
expect(window.location.pathname).toBe('/debug.html');
201+
expect(window.location.pathname).toBe(base);
197202
expect($url.path()).toBe('/');
198203
});
199204

@@ -207,7 +212,8 @@ describe('pushStateLocationService', () => {
207212
});
208213

209214
describe(origin + '/base/debug.html', () => {
210-
beforeEach(() => applyBaseTag(origin + '/base/debug.html'));
215+
const base = '/base/debug.html';
216+
beforeEach(() => applyBaseTag(origin + base));
211217

212218
it('handles bar correctly', () => {
213219
$url.url('bar');
@@ -229,15 +235,15 @@ describe('pushStateLocationService', () => {
229235

230236
it('handles / correctly', () => {
231237
$url.url('/');
232-
expect(window.location.pathname).toBe('/base/');
238+
expect(window.location.pathname).toBe(base);
233239
expect($url.path()).toBe('/');
234240
});
235241

236242
it('handles "" correctly', () => {
237243
$url.url('foobar');
238244
expect(window.location.pathname).toBe('/base/foobar');
239245
$url.url('');
240-
expect(window.location.pathname).toBe('/base/debug.html');
246+
expect(window.location.pathname).toBe(base);
241247
expect($url.path()).toBe('/');
242248
});
243249

0 commit comments

Comments
 (0)