fix(platform-server): read initial location from INITIAL_CONFIG if present

This commit is contained in:
Alex Rickabaugh 2017-02-14 19:48:48 -08:00 committed by Igor Minar
parent 047cda5b3c
commit 56f232cdd7
7 changed files with 63 additions and 23 deletions

View File

@ -7,13 +7,14 @@
*/
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
import {Inject, Injectable} from '@angular/core';
import {Inject, Injectable, Optional} from '@angular/core';
import {DOCUMENT} from '@angular/platform-browser';
import {Subject} from 'rxjs/Subject';
import * as url from 'url';
import {scheduleMicroTask} from './facade/lang';
import {getDOM} from './private_import_platform-browser';
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
@ -28,7 +29,16 @@ export class ServerPlatformLocation implements PlatformLocation {
private _hash: string = '';
private _hashUpdate = new Subject<LocationChangeEvent>();
constructor(@Inject(DOCUMENT) private _doc: any) {}
constructor(
@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);
this._path = parsedUrl.pathname;
this._search = parsedUrl.search;
this._hash = parsedUrl.hash;
}
}
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc); }
@ -59,7 +69,7 @@ export class ServerPlatformLocation implements PlatformLocation {
replaceState(state: any, title: string, newUrl: string): void {
const oldUrl = this.url;
const parsedUrl = url.parse(newUrl, true);
this._path = parsedUrl.path;
this._path = parsedUrl.pathname;
this._search = parsedUrl.search;
this.setHash(parsedUrl.hash, oldUrl);
}

View File

@ -7,7 +7,8 @@
*/
export {PlatformState} from './platform_state';
export {INITIAL_CONFIG, ServerModule, platformDynamicServer, platformServer} from './server';
export {ServerModule, platformDynamicServer, platformServer} from './server';
export {INITIAL_CONFIG, PlatformConfig} from './tokens';
export {renderModule, renderModuleFactory} from './utils';
export * from './private_export';

View File

@ -20,6 +20,7 @@ import {ALLOW_MULTIPLE_PLATFORMS, DebugDomRendererV2, DebugDomRootRenderer} from
import {SharedStylesHost, getDOM} from './private_import_platform-browser';
import {ServerRendererV2, ServerRootRenderer} from './server_renderer';
import {ServerStylesHost} from './styles_host';
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
function notSupported(feature: string): Error {
throw new Error(`platform-server does not support '${feature}'.`);
@ -65,23 +66,6 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
},
];
/**
* Config object passed to initialize the platform.
*
* @experimental
*/
export interface PlatformConfig {
document?: string;
url?: string;
}
/**
* The DI token for setting the initial config for the platform.
*
* @experimental
*/
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');
/**
* The ng module for the server.
*

View File

@ -0,0 +1,26 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {InjectionToken} from '@angular/core';
/**
* Config object passed to initialize the platform.
*
* @experimental
*/
export interface PlatformConfig {
document?: string;
url?: string;
}
/**
* The DI token for setting the initial config for the platform.
*
* @experimental
*/
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');

View File

@ -12,7 +12,8 @@ import {first} from 'rxjs/operator/first';
import {toPromise} from 'rxjs/operator/toPromise';
import {PlatformState} from './platform_state';
import {INITIAL_CONFIG, platformDynamicServer, platformServer} from './server';
import {platformDynamicServer, platformServer} from './server';
import {INITIAL_CONFIG} from './tokens';
const parse5 = require('parse5');

View File

@ -162,7 +162,6 @@ export function main() {
});
}));
describe('PlatformLocation', () => {
it('is injectable', async(() => {
const platform = platformDynamicServer(
@ -173,6 +172,19 @@ export function main() {
platform.destroy();
});
}));
it('is configurable via INITIAL_CONFIG', () => {
platformDynamicServer([{
provide: INITIAL_CONFIG,
useValue: {document: '<app></app>', url: 'http://test.com/deep/path?query#hash'}
}])
.bootstrapModule(ExampleModule)
.then(appRef => {
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
expect(location.pathname).toBe('/deep/path');
expect(location.search).toBe('?query');
expect(location.hash).toBe('#hash');
});
});
it('pushState causes the URL to update', async(() => {
const platform = platformDynamicServer(
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);

View File

@ -1,6 +1,12 @@
/** @experimental */
export declare const INITIAL_CONFIG: InjectionToken<PlatformConfig>;
/** @experimental */
export interface PlatformConfig {
document?: string;
url?: string;
}
/** @experimental */
export declare const platformDynamicServer: (extraProviders?: Provider[]) => PlatformRef;