diff --git a/modules/@angular/platform-server/src/location.ts b/modules/@angular/platform-server/src/location.ts index 896953c710..3f0783b6a8 100644 --- a/modules/@angular/platform-server/src/location.ts +++ b/modules/@angular/platform-server/src/location.ts @@ -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` @@ -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; @@ -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); diff --git a/modules/@angular/platform-server/test/integration_spec.ts b/modules/@angular/platform-server/test/integration_spec.ts index f176ad707a..57219e142b 100644 --- a/modules/@angular/platform-server/test/integration_spec.ts +++ b/modules/@angular/platform-server/test/integration_spec.ts @@ -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: '', 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: ''}}]);