fix(platform-server): default unspecified sections of the url to empty string (#14512)

This commit is contained in:
Alex Rickabaugh 2017-02-16 10:18:55 -08:00 committed by Igor Minar
parent 4a56b6e7f6
commit 612f120208
2 changed files with 23 additions and 2 deletions

View File

@ -17,6 +17,14 @@ import {getDOM} from './private_import_platform-browser';
import {INITIAL_CONFIG, PlatformConfig} from './tokens'; 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` * 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) { @Inject(DOCUMENT) private _doc: any, @Optional() @Inject(INITIAL_CONFIG) _config: any) {
const config = _config as PlatformConfig | null; const config = _config as PlatformConfig | null;
if (!!config && !!config.url) { if (!!config && !!config.url) {
const parsedUrl = url.parse(config.url); const parsedUrl = parseUrl(config.url);
this._path = parsedUrl.pathname; this._path = parsedUrl.pathname;
this._search = parsedUrl.search; this._search = parsedUrl.search;
this._hash = parsedUrl.hash; this._hash = parsedUrl.hash;
@ -68,7 +76,7 @@ export class ServerPlatformLocation implements PlatformLocation {
replaceState(state: any, title: string, newUrl: string): void { replaceState(state: any, title: string, newUrl: string): void {
const oldUrl = this.url; const oldUrl = this.url;
const parsedUrl = url.parse(newUrl, true); const parsedUrl = parseUrl(newUrl);
this._path = parsedUrl.pathname; this._path = parsedUrl.pathname;
this._search = parsedUrl.search; this._search = parsedUrl.search;
this.setHash(parsedUrl.hash, oldUrl); this.setHash(parsedUrl.hash, oldUrl);

View File

@ -185,6 +185,19 @@ export function main() {
expect(location.hash).toBe('#hash'); 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(() => { it('pushState causes the URL to update', async(() => {
const platform = platformDynamicServer( const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]); [{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);