Skip to content

Commit

Permalink
fix(platform-server): default unspecified sections of the url to empt…
Browse files Browse the repository at this point in the history
…y string (angular#14512)
  • Loading branch information
alxhub authored and matsko committed Feb 16, 2017
1 parent e871df4 commit 6ad3f5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions modules/@angular/platform-server/src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import {getDOM} from './private_import_platform-browser';
import {INITIAL_CONFIG, PlatformConfig} from './tokens';


function parseUrl(urlStr: string): {pathname: string, search: string, hash: string} {
const parsedUrl = url.parse(urlStr);
return {
pathname: parsedUrl.pathname || '',
search: parsedUrl.search || '',
hash: parsedUrl.hash || '',
};
}

/**
* Server-side implementation of URL state. Implements `pathname`, `search`, and `hash`
Expand All @@ -33,7 +41,7 @@ export class ServerPlatformLocation implements PlatformLocation {
@Inject(DOCUMENT) private _doc: any, @Optional() @Inject(INITIAL_CONFIG) _config: any) {
const config = _config as PlatformConfig | null;
if (!!config && !!config.url) {
const parsedUrl = url.parse(config.url);
const parsedUrl = parseUrl(config.url);
this._path = parsedUrl.pathname;
this._search = parsedUrl.search;
this._hash = parsedUrl.hash;
Expand Down Expand Up @@ -68,7 +76,7 @@ export class ServerPlatformLocation implements PlatformLocation {

replaceState(state: any, title: string, newUrl: string): void {
const oldUrl = this.url;
const parsedUrl = url.parse(newUrl, true);
const parsedUrl = parseUrl(newUrl);
this._path = parsedUrl.pathname;
this._search = parsedUrl.search;
this.setHash(parsedUrl.hash, oldUrl);
Expand Down
13 changes: 13 additions & 0 deletions modules/@angular/platform-server/test/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ export function main() {
expect(location.hash).toBe('#hash');
});
});
it('handles empty search and hash portions of the url', () => {
platformDynamicServer([{
provide: INITIAL_CONFIG,
useValue: {document: '<app></app>', url: 'http://test.com/deep/path'}
}])
.bootstrapModule(ExampleModule)
.then(appRef => {
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
expect(location.pathname).toBe('/deep/path');
expect(location.search).toBe('');
expect(location.hash).toBe('');
});
});
it('pushState causes the URL to update', async(() => {
const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
Expand Down

0 comments on commit 6ad3f5a

Please sign in to comment.