Skip to content

Commit fe962f6

Browse files
petebacondarwinIgorMinar
authored andcommitted
fix(aio): LocationService urls should never start with a slash
There is a weirdness in the Angular Location service. If the `baseHref` is only a single slash (`'/'`) then it changes it to be an empty string (`''`). The effect of this is that `Location.normaliseUrl(url)` does not strip off the leading slash from url paths. The problem is that the leading slash only appears on the initial Location path, and not on urls that arrive from subscribing to the Location service. This commit is a workaround this problem.
1 parent 04e1458 commit fe962f6

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎aio/src/app/documents/document.service.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class DocumentService {
6161
}
6262

6363
private computePath(url) {
64-
url = url.startsWith('/') ? url : '/' + url;
64+
url = '/' + url;
6565
url = url.endsWith('/') ? url + 'index' : url;
6666
return 'content/docs' + url + '.json';
6767
}

‎aio/src/app/shared/location.service.spec.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('LocationService', () => {
3232

3333
let initialUrl;
3434
service.currentUrl.subscribe(url => initialUrl = url);
35-
expect(initialUrl).toEqual('/next-url3');
35+
expect(initialUrl).toEqual('next-url3');
3636
});
3737

3838
it('should emit all location changes after it has been subscribed to', () => {
@@ -51,10 +51,10 @@ describe('LocationService', () => {
5151
location.simulatePopState('/next-url3');
5252

5353
expect(urls).toEqual([
54-
'/initial-url3',
55-
'/next-url1',
56-
'/next-url2',
57-
'/next-url3'
54+
'initial-url3',
55+
'next-url1',
56+
'next-url2',
57+
'next-url3'
5858
]);
5959
});
6060

@@ -78,15 +78,15 @@ describe('LocationService', () => {
7878
location.simulatePopState('/next-url3');
7979

8080
expect(urls1).toEqual([
81-
'/initial-url3',
82-
'/next-url1',
83-
'/next-url2',
84-
'/next-url3'
81+
'initial-url3',
82+
'next-url1',
83+
'next-url2',
84+
'next-url3'
8585
]);
8686

8787
expect(urls2).toEqual([
88-
'/next-url2',
89-
'/next-url3'
88+
'next-url2',
89+
'next-url3'
9090
]);
9191
});
9292
});

‎aio/src/app/shared/location.service.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ export class LocationService {
1010
get currentUrl() { return this.urlSubject.asObservable(); }
1111

1212
constructor(private location: Location) {
13-
this.urlSubject = new BehaviorSubject(location.path(true));
14-
this.location.subscribe(state => this.urlSubject.next(state.url));
13+
14+
const initialUrl = this.stripLeadingSlashes(location.path(true));
15+
this.urlSubject = new BehaviorSubject(initialUrl);
16+
17+
this.location.subscribe(state => {
18+
const url = this.stripLeadingSlashes(state.url);
19+
return this.urlSubject.next(url);
20+
});
1521
}
1622

1723
go(url: string) {
18-
url = this.location.normalize(url);
1924
this.location.go(url);
2025
this.urlSubject.next(url);
2126
}
27+
28+
private stripLeadingSlashes(url: string) {
29+
return url.replace(/^\/+/, '');
30+
}
2231
}

0 commit comments

Comments
 (0)